我在Base类中重载了 for (int i=0;i < al.size();i++){
String currentValue = al.get(i);
String[] partsOfCurrentValue = currentValue.split(",");
for (String part : partsOfCurrentValue) {
If (part.trim().startsWith("Content")) {
String[] partsOfContent = part.split(":");
String valueOfContent = partsOfContent[1];
Log.i("Value of element "+i, valueOfContent);
break;
}
}
}
运算符。但是,当我向Derived类添加额外的重载new
时,gcc编译器在Base类中找不到new
运算符。为什么呢?
最佳, 亚历克斯
new
gcc输出:
#include <stdlib.h>
template <class t> class Base {
public:
Base() {}
void * operator new (size_t size, void *loc) { return loc; }
};
template <class t> class Derived : public Base<t> {
public:
Derived() {}
void * operator new (size_t size, int sz, void *loc) { return loc; }
};
void foo() {
void *loc = malloc(sizeof(Derived<char>));
Derived<char> *d = new (loc) Derived<char>();
}
答案 0 :(得分:5)
通过展示位置operator new
表达式
new
时
new (loc) Derived<char>();
编译器在operator new
类(而不是Derived
类)中查找Base
的重载。它找到了,但你的重载
void * operator new (size_t size, int sz, void *loc) { return loc; }
// ^ additional parameter
接受更多参数,因此错误。
如果你问为什么编译器不够聪明,无法调用Base
operator new
的重载,那是因为name hiding:operator new
Derived
类中的重载隐藏了Base
类中的一个。如果您想在Base::operator new
课程中看到Derived
重载,请使用
using Base<t>::operator new;