Interview Question
Tech LeadsCountry: India
Interview Type: Phone Interview
for JAVA:
adding some line of code in finalize method can prevent an object to get deleted.
Make destructor of class private.
Write a friend function which takes the ownership if deleting object.
#include <iostream>
using namespace std;
class aa
{
public:
aa()
{
}
friend void del(aa* obj)
{
delete obj;
}
private:
~aa(){}
};
int main() {
// your code goes here
aa *a = new aa();
//delete a;
del(a);
return 0;
}
Our class can implement an interface which does have functionality only relevant to client and provide that interface to the client (through which it can only use the object but not delete it). And we will have to make class`s destructor private, so that client can not specifically 'delete' the object. But by doing this we adding a constraint that, object of the class has to be instantiated using 'new'.
- aniteshm11 November 13, 2013