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
Solved

Default Destructor ?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 197
No module found pypyodbc, 3 32
Need some help with mailto 16 20
C++ Code Issue 4 25
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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