-1

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;
}

DEMO

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++)?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 3
    i'd recommend not messing with new. just use std::unique_ptr and std::make_unique for heap objects – skeller Jun 18 '19 at 20:45
  • no not common at all, I am new in c++ since already some time and I never had to do something like that. Starting to learn c++ now you are in the fortunate situation that you probably wont have to use `new` directly for quite a while – 463035818_is_not_an_ai Jun 18 '19 at 20:51
  • @formerlyknownas_463035818 So I suppose it is some legacy code with new operators that should be avoided. Understood, thanks. – St.Antario Jun 18 '19 at 20:54
  • 1
    I've seen at least one large library (Xerces) that does something like this. All of its classes are derived from a base `Memory` class that has a custom `operator new`. Not saying its a _good_ idea, just that I've seen it done before. – Miles Budnek Jun 18 '19 at 21:54
  • 1
    total sidenote: you shouldn't use ` `and ``. C++ has alternative libraries with better functions also you aren't using them so you don't have a need to include them. – Tarick Welling Jun 19 '19 at 08:47
  • @TarickWelling Can you please elaborate what are these functions? How to allocate memory if not with malloc then? – St.Antario Jun 19 '19 at 08:55
  • You would use [containers](https://en.cppreference.com/w/cpp/container) and [smart pointers](https://en.cppreference.com/w/cpp/memory) in a perfect world but the [`new` and `delete`](http://www.cplusplus.com/doc/tutorial/dynamic/) functions are the better versions of `malloc` and `free`. `malloc` and `free` shouldn't be used in C++ because they don't invoke constructors and destructors. (among a myriad of problems with `malloc` and `free`. – Tarick Welling Jun 19 '19 at 08:59

1 Answers1

4

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.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312