When i develop a destructor, do i require to explicitly call, C/C++ Programming

When I develop a destructor, do I require to explicitly call the destructors for my member objects?

 

 

Posted Date: 3/15/2013 5:55:11 AM | Location : United States





Let''s work an instance. Imagine you need your constructor Foo::Foo(char) to call another constructor of the similar class, namely Foo::Foo(char,int), in order that Foo::Foo(char,int) would help initialize this object. Unluckily there''s no way to do this in C++.

Some of people do it anyway. Unluckily it doesn''t do what they want. For instance, the line Foo(x, 0); does not call Foo::Foo(char,int) on the this object. Rather then it calls Foo::Foo(char,int) to initialize temporary, local object (not this), then it instantly destructs that temporary while control flows over the ;.

class Foo { public: Foo(char x);

Foo(char x, int y);

...

};

 

Foo::Foo(char x)

{

...

Foo(x, 0); // this line does NOT help initialize the this object!!

...

}

You can combine sometimes two constructors through a default parameter:

class Foo {

public:

Foo(char x, int y=0); // this line combines the two constructors

...

};

If that doesn''t work, for example if there isn''t an suitable default parameter which combines the two constructors, every so often you can share their common code in a private init() member function:

class Foo { public: Foo(char x);

Foo(char x, int y);

... private:

void init(char x, int y);

};

 

Foo::Foo(char x)

{

init(x, int(x) + 7);

...

}

Foo::Foo(char x, int y)

{

init(x, y);

...

}

void Foo::init(char x, int y)

{

...

}

BTW do NOT attempt to get this via placement new. Some of the people think they can say new(this) Foo(x, int(x)+7) in the body of Foo::Foo(char). Constructors do a bunch of little magical things behind the scenes, but that bad technique steps on those partly constructed bits.

 

Posted by | Posted Date: 3/15/2013 5:55:55 AM


Related Discussions:- When i develop a destructor, do i require to explicitly call, Assignment Help, Ask Question on When i develop a destructor, do i require to explicitly call, Get Answer, Expert's Help, When i develop a destructor, do i require to explicitly call Discussions

Write discussion on When i develop a destructor, do i require to explicitly call
Your posts are moderated
Related Questions
Reference Types The symbol "&" is interpreted as an address operator as well as AND operator. This operator is also used to declare a "reference variable". A reference is refer

Write a ‘C’ program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

If you have computed a number (stored as a ?oating-point value) and want to display this on a plot (i.e. as a character string) you have to convert it into a STRING. That is we wan

Write a program to add co-ordinates of the plane The class having x and y co-ordinates. Create three objects. Use a constructor to pass one pair of co-ordinates and a funct

what is virtual datatype

A skilled programmer is required to build a standalone module to extract, parse and store in database tables, word count data from web pages and RSS feeds. The module will take

Develop Pidgin for Windows Project Description: I want to build Pidgin (open source instant messenger) for Windows with D-Bus support enabled. I've effectively built both

write an algorithm to swap values without using third variable

what is polymorphism

Conversion from Basic to User-Defined variable Consider the following example. class Distance                  {                   public  :