Manhattan associates Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
what's the matter of making destructor private ?
I think the easiest way is to provide a static func
to construct the objects of your class (whenever you like)
and declare the constructor private
In order to prevent clients to create object on heap, you would need to make new/delete, placement new/delete operators private. And make all constructor and destructors private to avoid object creation on stack by client. Now one can't create any instance of your object. So you would need to provide a static method for object creation/deletion.
Here is an example:
class myclass
{
private:
void* operator new(size_t); // standard new
void* operator new(size_t, void*); // placement new
void* operator new[](size_t); // array new
void* operator new[](size_t, void*); // placement array new
myclass() {}
~myclass(){}
public:
myclass & createObject()
{
return new myclass();
}
void destroyObject() const
{
delete this;
}
}
In order to prevent clients to create object on heap, you would need to make new/delete, placement new/delete operators private. And make all constructor and destructors private to avoid object creation on stack by client. Now one can't create any instance of your object. So you would need to provide a static method for object creation/deletion.
Here is an example:
class myclass
{
private:
void* operator new(size_t); // standard new
void* operator new(size_t, void*); // placement new
void* operator new[](size_t); // array new
void* operator new[](size_t, void*); // placement array new
myclass() {}
~myclass(){}
public:
myclass & createObject()
{
return new myclass();
}
void destroyObject() const
{
delete this;
}
}
In order to prevent clients to create object on heap, you would need to make new/delete, placement new/delete operators private. And make all constructor and destructors private to avoid object creation on stack by client. Now one can't create any instance of your object. So you would need to provide a static method for object creation/deletion.
Here is an example:
class myclass
{
private:
void* operator new(size_t); // standard new
void* operator new(size_t, void*); // placement new
void* operator new[](size_t); // array new
void* operator new[](size_t, void*); // placement array new
myclass() {}
~myclass(){}
public:
myclass & createObject()
{
return new myclass();
}
void destroyObject() const
{
delete this;
}
}
In order to prevent clients to create object on heap, you would need to make new/delete, placement new/delete operators private. And make all constructor and destructors private to avoid object creation on stack by client. Now one can't create any instance of your object. So you would need to provide a static method for object creation/deletion.
Here is an example:
class myclass
{
private:
void* operator new(size_t); // standard new
void* operator new(size_t, void*); // placement new
void* operator new[](size_t); // array new
void* operator new[](size_t, void*); // placement array new
myclass() {}
~myclass(){}
public:
myclass & createObject()
{
return new myclass();
}
void destroyObject() const
{
delete this;
}
}
Only on Stack. Make new operator private.
- Dr.Sai December 05, 2011Only on Heap. Make Destructor private.