Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Polymorphism

Reply
Thread Tools

Polymorphism

 
 
Student911
Guest
Posts: n/a
 
      01-31-2005

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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-31-2005
Student911 wrote:
> 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
 
Reply With Quote
 
 
 
 
rogero@howzatt.demon.co.uk
Guest
Posts: n/a
 
      01-31-2005

Student911 wrote:
> 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
--
MVP in C++ at www.brainbench.com

 
Reply With Quote
 
Richards Noah \(IFR LIT MET\)
Guest
Posts: n/a
 
      02-01-2005
"Student911" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
Polymorphism in GridView (ASP.Net 2.0) Kevin Lawrence ASP .Net 0 02-16-2006 11:32 AM
polymorphism Tony Morris Java 1 03-07-2004 01:29 AM
Help : Casting after a clone & method polymorphism Osaman Java 2 01-05-2004 03:59 PM
Polymorphism in xml ?? Sony Antony Java 0 08-26-2003 03:37 PM



Advertisments