Polymorphism

S

Student911

Hello,

I read that upward casting is always safe but downward casting/
For example:
Derived* p=(Derived*) new Base();

casting can cause problems with the memory. Can someone please give me
an example that memory problems are caused?
I thought that problems will happen during upward casting since if
Person has _name and _id and programmer is derived from person and has
_company is another member then the casting:
Person *p= new Programmer("Greg Smith",954954,Intel);
Then this casting will cause the lost of the _company member, but I was
told it won't happen. Can someone explain that as well?
Thanks in advance
 
V

Victor Bazarov

Student911 said:
I read that upward casting is always safe but downward casting/
For example:
Derived* p=(Derived*) new Base();

casting can cause problems with the memory. Can someone please give me
an example that memory problems are caused?

I don't know about memory problems, but using 'p' from your code snippet
should cause undefined behaviour IIUIC.

You cannot simply cast a pointer to base to a pointer to derived and
expect it to go unnoticed. If you try to use any members of 'Derived'
that are not present in 'Base', you're in for a surprise. If you're
trying to use any member of 'Derived' and 'Base' is not the first base
class of 'Derived', you're in for a surprise. You are in for a whole
different surprise if 'Base' is a virtual base class.
I thought that problems will happen during upward casting since if
Person has _name and _id and programmer is derived from person and has
_company is another member then the casting:
Person *p= new Programmer("Greg Smith",954954,Intel);
Then this casting will cause the lost of the _company member, but I was
told it won't happen. Can someone explain that as well?

Converting a pointer to a derived class object to a pointer to base is OK
since all that conversion does is finding the address of the base class
object inside the derived class object. The object itself doesn't go
anywhere. What you heard about is called "slicing" and that will
definitely happen if you instead of using pointers use objects:

Programmer pr("Greg Smith",954954,Intel);
Person p = pr; // assuming that 'Programmer' derives from 'Person'

in this case only the 'Person' part of 'pr' is copied into 'p'. The
company has nowhere to go since Person has no member for it.

I bet you will find more about it in your favourite C++ book.

V
 
R

rogero

Student911 said:
Hello,

I read that upward casting is always safe but downward casting/ [snip]
I thought that problems will happen during upward casting since if
Person has _name and _id and programmer is derived from person and has
_company is another member then the casting:
Person *p= new Programmer("Greg Smith",954954,Intel);

Another view on this is that every programmer is a person
but not every person is a programmer [ thank goodness :) ]

So you can always treat a programmer as a person:
Person *person = (Person*)programmer;
but you can't treat every person as a programmer:
Programmer *programmer = (Programmer*)person;
HTH
Roger Orr
 
R

Richards Noah \(IFR LIT MET\)

Student911 said:
Hello,

I read that upward casting is always safe but downward casting/
For example:
Derived* p=(Derived*) new Base();

casting can cause problems with the memory. Can someone please give me
an example that memory problems are caused?
I thought that problems will happen during upward casting since if
Person has _name and _id and programmer is derived from person and has
_company is another member then the casting:
Person *p= new Programmer("Greg Smith",954954,Intel);
Then this casting will cause the lost of the _company member, but I was
told it won't happen. Can someone explain that as well?
Thanks in advance

The problem is what is referred to as "slicing", or, as my past professors
called it, "superclass truncation".

The idea, conceptually, is that derived classes appear (in memory) as the
base class with the extra variables tacked on at the end (this is a bit of a
simplification). This is fine for things like:

Base b;
Derived d;
d = b;

The problem occurs when you have:
b = d;

C++ will "slice" off the portion of the Derived class that is not found in
the Base class, in order to fit it into the space allocated to an object of
type Base.

This isn't a problem with the example you showed in your post, as assigning
pointers doesn't change the internal representation of the data. The
problem occurs only when you try to fit a Derived type into the memory
allocated to a Base type.

http://en.wikipedia.org/wiki/Slicing
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top