I have overloaded new
operator in the Base class. However, when I add additional overloaded new
to the Derived class gcc compiler does not find new
operator in the Base class. Why?
Best, Alex
#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>();
}
gcc output:
new.cpp: In function ‘void foo()’:
new.cpp:17:45: error: no matching function for call to ‘Derived<char>::operator new(sizetype, void*&)’
Derived<char> *d = new (loc) Derived<char>();
^
new.cpp:17:45: note: candidate is:
new.cpp:11:10: note: static void* Derived<t>::operator new(size_t, int, void*) [with t = char; size_t = unsigned int]
void * operator new (size_t size, int sz, void *loc) { return loc; }
^
new.cpp:11:10: note: candidate expects 3 arguments, 2 provided