Solved

Default Destructor ?

Posted on 2003-12-07
5
762 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 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

20 Experts available now in Live!

Get 1:1 Help Now