468,306 Members | 1,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

Inheritance + Object in Memory

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
Jun 27 '08 #1
6 1007
Slain wrote:
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?

--
Ian Collins.
Jun 27 '08 #2
A or C ?

On Apr 17, 5:44 pm, Ian Collins <ian-n...@hotmail.comwrote:
Slain wrote:
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?

--
Ian Collins.
Jun 27 '08 #3
Slain wrote:

[please don't top-post]
On Apr 17, 5:44 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Slain wrote:
>>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?
[or quote signatures]
A or C ?
"new C()" can only return a C.

--
Ian Collins.
Jun 27 '08 #4
Slain wrote:
A or C ?
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 !
}
Jun 27 '08 #5
Gianni Mariani wrote:
This below returns a reference to the A part of the new's C.

A & f()
{
return * new C(); // careful on how you delete !
}
But as long as ~A() is virtual, it should delete OK shouldn't it???
Jun 27 '08 #6
James Connell wrote:
Gianni Mariani wrote:
>This below returns a reference to the A part of the new's C.

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

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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jeff Greenberg | last post: by
4 posts views Thread by JKop | last post: by
22 posts views Thread by Matthew Louden | last post: by
4 posts views Thread by john andrew | last post: by
5 posts views Thread by relient | last post: by
47 posts views Thread by Mark | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.