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
Write a Haskell program that calculates a balanced partition of N items where each item has a value between 0 and K such that the difference between the sum of the values of first

Define Commonly Used Built-in Library Functions? Comprise opened a file pointer you will desire to use it for either input or output. The C language supplies a set of functions

i need a program that generates this output: The highest card wins! Suit Order is: clubs, diamonds, hearts and spades The computers card is the 5 of Spades. The player’s card is t

I am seeking an expert to create a 360 degree virtual tour in KRPano for use on PCs, tablets and smartphones. You will be given with panoramas in order to create an advanced KRPano

#question.Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two po

A: Use operator overloading to present a friend right-shift operator, operator>>. It is similar to the output operator, except the parameter doesn't contain a const: "Fred&" instea

- Create a program that uses at least two functions that will be called from your main. This program is a number game program that asks for parts of your phone and after manipulati

We need an E Commerce site likefifacointrader. you must have experience with that kind of work You have to show a sample of work like that site. Skills required are C Prog

The Preprocessor Directives A preprocessor directive which starts with a hash '#' ,is an instruction to the preprocessor, which acts on the source code before the compilation p

write a c program to find the area under the curve4 f=(x) between x=a and x=l integrate y=(x) between the limits of a and b Solution: #include float    start_point,