I'm very new in C++ and was confused by the following example:
#include <iostream>
#include <cstddef>
#include <cstdlib>
class Test{
public:
enum test_type{ test1 = 0, test2, test3};
void *operator new(size_t sz, test_type t){
return malloc(sz);
}
};
class SomeClass: public Test{
public:
SomeClass(int a): a(a){}
int a;
};
int main(int argc, char ** argv){
SomeClass *sc = new(Test::test1) SomeClass(10);
std::cout << sc->a << std::endl;
}
I read name lookup rules in the cpp-reference page, but did not find any explanation about why the operator new defined in the base class was found and used for derived class (which can potentially contain an additional member).
Is it a common way to declare operator new
in a base class and then use it to allocate memory for derived classes (the idea was taken from some open-source project written in C++)?
The allocation function operator new
is "looked up in the scope of" the class being created, in this case SomeClass
. When you look up a name in the scope of a class, it can find the name as a member of a base class. That's what happens in this case.
Perhaps you're wondering why the lookup of operator new
does not use some special rule that excludes base class members from consideration. Well, I think in some cases using the base class operator new
makes sense. The fact that the derived class may contain an additional member does not, in itself, cause a problem, since the language will pass the size of the derived class to operator new
, ensuring that it knows how much memory it has to allocate for the derived class object. And using the base class operator new
allows said operator to be brought in from a CRTP base class, or simply to provide a convenient way for derived classes to be allocated from the same pool.