Inheritance + Object in Memory

S

Slain

I have this question about what type of object is a memory block when
you have inheritance going on

Class A
{
blah blah blah
}.

Class B : public Class A
{
blah blah blah
}

Class C : public Class B
{
..
..
static A *f1();

};

A* C::f1()
{
return new C();
}

The memory allotted by the line "return new C()", would be what kind
of object?

Thanks in anticipation
 
I

Ian Collins

Slain said:
I have this question about what type of object is a memory block when
you have inheritance going on

Class A
{
blah blah blah
}.

Class B : public Class A
{
blah blah blah
}

Class C : public Class B
{
..
..
static A *f1();

};

A* C::f1()
{
return new C();
}

The memory allotted by the line "return new C()", would be what kind
of object?
A C, what else would you expect?
 
G

Gianni Mariani

Slain said:

a C is an A as well as a C. So the answer is both.

A f()
{
return * new C(); // leak !
}

This above returns a copy of the slice of C that is an A and only an A.

This below returns a reference to the A part of the new's C.

A & f()
{
return * new C(); // careful on how you delete !
}
 
K

Kai-Uwe Bux

James said:
But as long as ~A() is virtual, it should delete OK shouldn't it???

Provided you can get a pointer to the object. Since a reference is returned,
you would need to convert to a pointer first (e.g., using operator& if that
has not been overloaded). I think that is what the comment refers to.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top