Goldman Sachs Interview Question
Developer Program EngineersI think no need of step 2.
This will work.
#include <iostream>
using namespace std;
class A
{
private:
int a;
~A(){cout<<"~A()\n";}
public:
A(){cout<<"A()\n";}
};
int main ()
{
A *pa = new A;
A a;
return 0;
}
In main line 3 will give compilation error.
But here the problem is once the object is created on the stack, it cant be deleted.
Does somebody have a better technique.
slight modification to your code will do the job;
#include <iostream>
using namespace std;
class A{
private:
~A(){ cout << " going in " << endl;}
public:
A(){ cout << going out " << endl;}
void ClearA(){ delete this;}
};
int main()
{
A *a = new A;
a->ClearA();
return 0;
}
Cool solutions!
Could you pls explain why it's not possible to create object of "class A" on stack when the destructor is declared as private? Thanks in advance.
#include<iostream>
using namespace std;
class A
{
public:
A();
private:
};
A::A()
{
cout<<"In constrcutor"<<endl;
}
int main()
{
A a;
A b;
cout<<"Address of a is"<<&a; // o/p is 0XXX0003a -> Means this was allocated on the Stack
cout<<"Address of b is"<<&b; // o/p is 0XXX0003b -> Means this was allocated on the Stack
A *c= new A();
cout<<"Address of c is"<<&c; // o/p is 0XXX00024 -> Means this was allocated on the Heap
}
#include<iostream>
using namespace std;
class A
{
public:
A();
private:
~A();
};
A::~A()
{
cout<<"Ïn destructor"<<endl;
}
A::A()
{
cout<<"In constrcutor"<<endl;
}
int main()
{
char buf[2]; --> Allocated on stack
A a; --> Throws an error because when the local object goes out of scope immidietely linker will call the destructor but here it is pvt
A *c=new(buf) A(); -> Allocated on the heap
delete c; --> throws an error bcz destructor is pvt
cout<<"Address of a is "<<&c<<endl; --> // o/p is 0XXX00024 -> Means this was allocated on the Heap(this is the heap address , see the above pgm
cout<<"Address of a is "<<&buf<<endl;
getchar();
return 0;
}
In order to release the memory we should have to write our own user define function. Any way we can't free the memory directly in C++ either we should have to call delete/user-defined function.
Hope this helps
make the constructors private. This will restrict the creation of an object on the stack. The heap object however can be created by the new which can call the constructor.
The above is a simplest case.
An advanced case may be to make the destructor private. As constructors may have different signatures while destructor's signature is unique, it will always be hepful.
its not true. I can still make object on stack by using:
#include <iostream>using namespace std;
class A{ private: ~A(){ cout << " going in " << endl;} public: A(){ cout << going out " << endl;} void ClearA(){ delete this;}};
int main(){ char buf[10000]; A *a = new (buf)A; return 0;}
buf is on stack which a is create on this address..
this solution can have portability problems related to alignment .
i think if we make the constructor also private and expose some static public instantiation methods , which will create the object using new ,then that solution is perfect .
How about keeping all the constructors as private and just giving the placement new operator as public.
Pritesh suggestion is in line with the more traditional solution and is called the 'Named Constructor Idiom.'
Search the net for the C++ FAQ by Marshall Cline. Check out FAQ [16.21] which answers the following question.
"How can I force objects of my class to always be created via new rather than as locals or global/static objects?"
This technique is also commonly used with the 'Factory Pattern.'
Isn't Singleton class create object on the heap
1. Protected/Private Constructor
2. Static GetInstance method to initialize the object
3. public Destructor
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() { }
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
Isn't Singleton class create object on the heap
1. Protected/Private Constructor
2. Static GetInstance method to initialize the object
3. public Destructor
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() { }
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
1)make the destructor as a private member function
- pritesh.kumar2 June 20, 20112)add a public function which will have this statement :
delete this;