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
Different implementations of OOP Object-oriented programming is not mainly concerned with the details of the program operation. Instead, it deals with the overall design of the

Multiple Inheritance Multiple inheritance , as the name suggests , is  deriving a class from more than one class . The derived class inherits all the properties of all its base c

Is always the default constructor for Fred Fred::Fred()?

There are two ways for initializes in C++ as shown in the example that follows. The first way uses the traditional C notation. The second way uses constructor notation. int foo

Create a function ValueDelta(char inName[], char outName[]) that reads a text file with option specifications and writes the option values as well as Delta. The inName[] file conta

i have an array of structs, and am trying to make a pointer that can point to a certain struct within the array

Armed with your function from above, we can do some interesting things. For instance, any pixel where the offsets are both zero is a pit (lower than all surrounding points) .

Project Description: Boots capture video from your YouTube channel and upload your own video to our channel or the copyright of the videos, bang, 3 parties, automatically delete

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Q: How can Define an Array? An Array is defined in much the alike manner as ordinary variables except that every array name must be accompanied by a size specification (that is