Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public new private constructor

Tags:

c++

When I try compiling the following:

#include <iostream>

class Test
{
public:
    void* operator new (size_t num);
    void operator delete (void* test);
    ~Test();
private:
    Test();
};

Test::Test()
{
    std::cout << "Constructing Test" << std::endl;
}

Test::~Test()
{
    std::cout << "Destroying Test" << std::endl;
}

void* Test::operator new (size_t num)
{
    ::new Test;
}

void Test::operator delete(void* test)
{
    ::delete(static_cast<Test*>(test));
}

int main()
{
    Test* test = new Test;
    delete test;
}

I get :

$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:14: error: ‘Test::Test()’ is private
test.cpp:36: error: within this context

If the new is a member function, why can it not call the private constructor?

Edit: My idea is to create a class that can only be instantiated on the heap using totally standard syntax. I was hoping since new is a data member, it could call the private constructor but since new is not used for stack objects, you would not be allowed to create the object on the stack.

like image 515
doron Avatar asked Feb 01 '10 17:02

doron


People also ask

Can we create public and private constructor same class?

We can't create public and private constructors simultaneously in a class, both without parameters. We can't instantiate the class with a private constructor. If we want to create an object of a class with private constructor then, we need to have public constructor along with it.

Can public class have private constructor?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.

What is the difference between private and public constructor?

private constructor is used to prevent a class to be instantiated. But, if a class has other public constructors, then that can be instantiated. A class can have multiple private constructor and can call it by another constructor. public A(int b) : A() // Calling private constructor by another constructor.

Should constructors be public or private?

Constructors in programming are the methods that are called automatically when an object is initialized. The constructor's purpose is to initialize the object. Constructors should always be public and they are declared without any return type.


1 Answers

I think you have a misunderstanding on what the operator new does. It does not create objects, but rather allocates memory for the object. The compiler will call the constructor right after calling your operator new.

struct test {
   void * operator new( std::size_t size );
};
int main()
{
   test *p = new test;
   // compiler will translate this into:
   //
   // test *p = test::operator new( sizeof(test) );
   // new (static_cast<void*>(p)) test() !!! the constructor is private in this scope
}

The main usage of the operator new is having a memory allocator different to the default allocator for the system (usually malloc), and it is meant to return an uninitialized region of memory on which the compiler will call the constructor. But the constructor is called after the memory is allocated in the scope where the new call was written (main in this case).

After acceptance note

The complete solution to the unformulated question: how do I force users of my class to instantiate in the heap? is to make the constructors private and offer a factory function, as shown in some other answers (as the one by villintehaspam) point out.

like image 58
David Rodríguez - dribeas Avatar answered Sep 28 '22 13:09

David Rodríguez - dribeas