Solved

Default Destructor ?

Posted on 2003-12-07
5
771 Views
Last Modified: 2012-08-14
hello experts, please tell me as c++ compiler supplies a default constructor if none is specified by user, does it in the same way provide a destructor also that will destroy the objects ?
if not, then when will those objects destroyed and when the memory alloacted to them will be dealloacted.?
thanks.
Sandeep.
0
Comment
Question by:soodsandeep
5 Comments
 
LVL 2

Expert Comment

by:sin_
ID: 9894614
hi sandeep,

the compiler supplies a default ctor, and a default dtor too.

say, when you have a class declaration like this:

class test {
int a;
public:
void foo() { }
};

the default ctor and dtor have been created for you by the compiler. But the only problem is, it doesn't initialize your data members.

In a case like this:
class test1 {
char* p;
public:
void foo() {}
};

the default ctor won't initialize your char*, so you need to provide a ctor which takes care of that; same with the dtor. It won't destruct the char* contained inside the object, when the default dtor is called.


hope this helps
0
 
LVL 30

Expert Comment

by:Axter
ID: 9894888
Just to add to above comment, the default destructor will delete the pointer (char*) p, but it will not delete what p is pointing to.

In general, anything that is create via new or malloc in the constructor, will not be deleted or free in the default destructor.
0
 
LVL 10

Expert Comment

by:Duy Pham
ID: 9897253
Default destructor does not release content of poiter attributes. That means if you have a member property is a pointer, you must do the delete or free yourself. Only reference variables are deleted automatically with default destructor.

In, C++ when you did not specify a default contructor will be created for you. C++ classes have two types of constructor : Default Constructor and Default Copy Constructor which will be automatically created when you do not implement them. But when you define a constructor, the default will be omitted.

Remember that, default copy constructor and default destructor are only good if there is no pointer member in your class, because defaults only copy or delete which the class contains, not links ( I mean pointers are links ).
0
 

Author Comment

by:soodsandeep
ID: 9902391
Hello PDuy !!
thanks for your reply. could you please explain the first sentence "Default destructor does not release content of poiter attributes".
thanks.
0
 
LVL 10

Accepted Solution

by:
Duy Pham earned 50 total points
ID: 9910551
Hi soodsandeep, please see the sample below :

class A
{
    public void Print() { printf("I am A object !\n"); }
};

class B
{
   A* pA;

   public B()
   {
        pA = new A();
   }

   public void Print()
   {  
      printf("I am B object ! I have a property as pointer to A object \n");
      pA->Print();
   }
};

In this case, if you create a B object and delete it, only the 4 bytes of pA were deleted by default destructor. But the A object that pA linked to stayed still in memory and was not deleted. OK ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now