Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Do we have private destructors?

Answers were Sorted based on User's Feedback



Do we have private destructors?..

Answer / sampurna pandey

yes, we can have both private constructor and destructor
and can use them with help of static function for example

class Base
{
static int i;
Base(){}
~Base(){}
public:
static Base *Creat_Object();
static void Destroy_Object(Base *p);
void Add()
{
cout<<i<<" Add function"<<endl;
}
};

int Base::i=0;
Base* Base::Creat_Object()
{
Base * ptr = new Base();
i=5;
return ptr;
}

void Base::Destroy_Object(Base *p)
{
delete p;
}

void main()
{
Base *temp = Base::Creat_Object();
temp->Add();
Base::Destroy_Object(temp);
}

correct me if i am wrong.

Is This Answer Correct ?    21 Yes 3 No

Do we have private destructors?..

Answer / guest

RE: #1

You can have private constructors. It's pretty common
practice when you create a class which you want to never be
constructed with "new" from outside the class (for example,
if you have a static factory member of the class).

Is This Answer Correct ?    10 Yes 1 No

Do we have private destructors?..

Answer / vaibhav meena

We can have Private Destructor and by using Friend Function
we can also instantiate it. :)

Is This Answer Correct ?    6 Yes 0 No

Do we have private destructors?..

Answer / subrat

Hi all,

Please visit this url for more clarity on "Private
destructors"

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c414
3/

Is This Answer Correct ?    1 Yes 0 No

Do we have private destructors?..

Answer / binoy mathew

#include <iostream>
#include <stdlib.h>

class t
{
public:
t()
{
printf("in constr");
}

private: // note that constructor is private here
~t()
{
printf("in destr");
}
};


int main()
{
t *t1 = new t; // create a new obj
delete t1; // delete the obj, which calls destructor
return 0;
}

Try to compile the above code.
following error results...

[root@localhost Desktop]# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13: error: ‘t::~t()’ is private
tt.cpp:: error: within this context
[root@localhost Desktop]#

....implies, we can't have destructor private.

Is This Answer Correct ?    1 Yes 3 No

Do we have private destructors?..

Answer / shakti singh khinchi

U can't instantiate instance of that class.

Is This Answer Correct ?    0 Yes 3 No

Do we have private destructors?..

Answer / dhananjay

no

Is This Answer Correct ?    2 Yes 5 No

Do we have private destructors?..

Answer / vishwa

we cannot say it destructors are not private. because for
empty class if we use delete operator then it is going to
delete the destructor which is private.

Is This Answer Correct ?    0 Yes 8 No

Do we have private destructors?..

Answer / thuang

In replay to Vishwa,

for empty class, compiler will supply a default constructor
and a destructor for us, their access modifiers were public.

Is This Answer Correct ?    0 Yes 8 No

Do we have private destructors?..

Answer / pooja

no we do not have private destructors because the
destructors can be declared only in public access
specifier.

Is This Answer Correct ?    1 Yes 11 No

Post New Answer

More C++ General Interview Questions

What is a volatile variable in c++?

2 Answers  


How would you use the functions randomize() and random()?

0 Answers  


print first nodd numbers in descending order

7 Answers  


What is iostream in c++ used for?

0 Answers  


What is exception handling? Does c++ support exception handling?

0 Answers  


What is a static element?

0 Answers  


Am studying basic c++ programming, have been given the following assignment. Design a linear program to calculate the maximum stress a material can withstand given a force and a diameter of a circle. To find the required area pi should be defined. Have most of the program sorted out but am at a loss as to how to show the calculations required. Can anyone help?

0 Answers  


What new()is different from malloc()?

0 Answers  


When volatile can be used?

3 Answers   Symphony,


What is nested class in c++?

0 Answers  


Write a program to concatenate two strings.

0 Answers  


Give example of a pure virtual function in c++?

0 Answers  


Categories