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

Assignment Help:

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

 

 


Related Discussions:- When i develop a destructor, do i require to explicitly call

Write a program of constructors and destructors, Write a program of constru...

Write a program of constructors and destructors Make a class drugs having encapsulated data for medicine name, whether solid or liquid, price and purpose of use. From this clas

Hwid spoofer for windows 7, HWID Spoofer for Windows 7, 8 x64 Project De...

HWID Spoofer for Windows 7, 8 x64 Project Description: I want a simple program that will spoof my hardware id. I should be able to prepare the id to spoof to in the program a

C program to demonstrate pointer to variable, C program to demonstrate Poin...

C program to demonstrate Pointer to variable: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 //poin

C program to calculate area of cube, Aim: To implement program to calculat...

Aim: To implement program to calculate area of cube using inline function. Code: inline void area_cube(float side) {             float area;             are

Inheitance, ambiguity in multiple inheritance

ambiguity in multiple inheritance

I need asp .net facebook app project completion, I need Asp .net Facebook a...

I need Asp .net Facebook app project completion Project Description: We are urgently seeking a few remaining items to be completed, and the project to be deployed to live.

What are the additional keywords in c++, Additional keywords in C++ Cla...

Additional keywords in C++ Class     friend    virtual   inline private  public    protected     const this         new       delete   operator The actual use and expl

Networking program development, Networking program development. 1.ARP pr...

Networking program development. 1.ARP protocol. 2.Switching HUB. 3.wireshark. 4.winpcap library. 5.C++ & MFC. 6.LAN evironment through switch and HUB(static ARP t

Structures, please answer the question of following Write a function that c...

please answer the question of following Write a function that calculates the number of elapsed days between two dates. For example the days between Feb 3, 1970 and June 21, 1980? B

3/15/2013 5:55:55 AM

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.

 

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd