Solved

Default Destructor ?

Posted on 2003-12-07
5
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 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