Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Default Destructor ?

Posted on 2003-12-07
5
Medium Priority
?
841 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

662 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