473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Destructor thru parent class

class parent{

Parent(){};
~Parent(){};
}

Child: public Parent{

Child(){};
~Child(){};
}
void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;

destroy( c_ptr);
destroy(p_ptr);

exit 0;
}

I think this question must have been discussed to death but here it
goes again.

What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?

Also, what is the more general problem of doing such a delete? i.e.
deleting a child object pointed to by a parent pointer?
Thanks,
Sashi

Nov 30 '06 #1
6 3960
Sashi wrote:
class parent{

Parent(){};
~Parent(){};
}

Child: public Parent{

Child(){};
~Child(){};
}
void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;

destroy( c_ptr);
destroy(p_ptr);

exit 0;
}

I think this question must have been discussed to death but here it
goes again.
Indeed, that's why it's in the FAQ.
What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?

Also, what is the more general problem of doing such a delete? i.e.
deleting a child object pointed to by a parent pointer?
http://www.parashift.com/c++-faq-lit....html#faq-20.7

Cheers! --M

Nov 30 '06 #2
Sashi wrote:
class parent{
class Parent {
>
constructor and destructor are private
make them public
Parent(){};
~Parent(){};
you don't need semicolons here
}
you need one here
>
Child: public Parent{
class Child: public Parent{
>
same as above, use public
Child(){};
~Child(){};
semicolons!
}
semicolon!
>
void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;
Child * c_ptr = new Child;
Parent * p_ptr = new Parent;
>
destroy( c_ptr);
destroy(p_ptr);

exit 0;
return 0;
}

I think this question must have been discussed to death but here it
goes again.

What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?
new dynamically allocates memory and calls object's constructor.
there are dynamically allocated Child and Parent objects

add

std::cout << "~Parent\n";

to Parent's destructor and

std::cout << "~Child\n";

to Child's destructor and try it. then declare Parent's destructor
virtual

virtual ~Parent()

and try again.

Nov 30 '06 #3

dasjotre wrote:
Sashi wrote:
class Parent {
class Child: public Parent{
The names you use possibly reveal some misunderstanding of inheritance
on your side. Considering that in your example Child is a kind of a
Parent, which doesn't seem right. In real life all parents are
somebody's children and some children are also parents.

Check http://www.parashift.com/c++-faq-lit...heritance.html

Nov 30 '06 #4

dasjotre wrote:
Sashi wrote:
class parent{
class Parent {

constructor and destructor are private
make them public
Parent(){};
~Parent(){};

you don't need semicolons here
}

you need one here

Child: public Parent{
class Child: public Parent{

same as above, use public
Child(){};
~Child(){};

semicolons!
}

semicolon!

void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;

Child * c_ptr = new Child;
Parent * p_ptr = new Parent;

destroy( c_ptr);
destroy(p_ptr);

exit 0;

return 0;
}

I think this question must have been discussed to death but here it
goes again.

What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?

new dynamically allocates memory and calls object's constructor.
there are dynamically allocated Child and Parent objects

add

std::cout << "~Parent\n";

to Parent's destructor and

std::cout << "~Child\n";

to Child's destructor and try it. then declare Parent's destructor
virtual

virtual ~Parent()

and try again.
Ah, yes, apologies for the imperfect syntax. It's more like pseudocode
to illustrate a point.

My question simply boils down to this: what will happen when you call a
delete on the parent class pointer that actually points to a child
object?
I was asked this in an interview yesterday. I used to think I know c++
reasonably well and my inability to answer this question caused me
quite a bit of embarrassment.
The answer: "undefined behavior"
-Sashi

Nov 30 '06 #5

Sashi wrote:
dasjotre wrote:
Sashi wrote:
class parent{
class Parent {
>
constructor and destructor are private
make them public
Parent(){};
~Parent(){};
you don't need semicolons here
}
you need one here
>
Child: public Parent{
class Child: public Parent{
>
same as above, use public
Child(){};
~Child(){};
semicolons!
}
semicolon!
>
void destroy( Parent *ptr)
{delete ptr;}
>
int main(int argc, char** argv){
>
Child c_ptr = new Child;
Parent p_ptr = new Parent;
Child * c_ptr = new Child;
Parent * p_ptr = new Parent;
>
destroy( c_ptr);
destroy(p_ptr);
>
exit 0;
return 0;
}
>
I think this question must have been discussed to death but here it
goes again.
>
What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?
new dynamically allocates memory and calls object's constructor.
there are dynamically allocated Child and Parent objects

add

std::cout << "~Parent\n";

to Parent's destructor and

std::cout << "~Child\n";

to Child's destructor and try it. then declare Parent's destructor
virtual

virtual ~Parent()

and try again.

Ah, yes, apologies for the imperfect syntax. It's more like pseudocode
to illustrate a point.

My question simply boils down to this: what will happen when you call a
delete on the parent class pointer that actually points to a child
object?
I was asked this in an interview yesterday. I used to think I know c++
reasonably well and my inability to answer this question caused me
quite a bit of embarrassment.
The answer: "undefined behavior"
The question is whether you want it this way or not.

Nov 30 '06 #6
Sashi wrote:
My question simply boils down to this: what will happen when you call a
delete on the parent class pointer that actually points to a child
object?
I was asked this in an interview yesterday. I used to think I know c++
reasonably well and my inability to answer this question caused me
quite a bit of embarrassment.
The answer: "undefined behavior"
Or what the FAQ I cited calls "Yuck."

Cheers! --M

Nov 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Prawit Chaivong | last post by:
Hi All, There is code here. ------------------------------------------------------------------ class Base{ public: Base(){} virtual ~Base(){} private: int a;
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: yccheok | last post by:
hello, in a singleton design, i trying to make my parent class having protected constructor and destructor. however, the compiler give me error when my child, trying to delete itself through parent...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
4
by: iTooo | last post by:
What a subject, mhhh not resumable. Here is my problem: I'm using a template class for lists, upon deletion it deletes its elements, ok, works fine. Trouble comes when I put in this list...
1
by: balkanese | last post by:
Hi everybody! I encountered the following problem: I created a "class library (.NET)" project and made a first build, which produced a warning: > nochkclr.obj : warning LNK4099: PDB...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
15
by: vivekian | last post by:
Hi, I have this following class class nodeInfo ; class childInfo { public: int relativeMeshId ;
2
by: kasthurirangan.balaji | last post by:
Hello, template<class Base> class Derived : public Base { }; By using template, i understand the actual base type will be deduced at compile time. Moreover, class Derived will consist only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.