Polymorphism

Discussion in 'C++' started by Student911, Jan 31, 2005.

  1. Student911

    Student911 Guest

    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
     
    Student911, Jan 31, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jan 31, 2005
    #2
    1. Advertising

  3. Student911

    Guest

    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
     
    , Jan 31, 2005
    #3
  4. "Student911" <> wrote in message
    news:...
    >
    > 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
     
    Richards Noah \(IFR LIT MET\), Feb 1, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Lawrence

    Polymorphism in GridView (ASP.Net 2.0)

    Kevin Lawrence, Feb 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    853
    Kevin Lawrence
    Feb 16, 2006
  2. Sony Antony

    Polymorphism in xml ??

    Sony Antony, Aug 26, 2003, in forum: Java
    Replies:
    0
    Views:
    567
    Sony Antony
    Aug 26, 2003
  3. Osaman
    Replies:
    2
    Views:
    453
    John C. Bollinger
    Jan 5, 2004
  4. Tony Morris

    polymorphism

    Tony Morris, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    735
    sonali
    Mar 7, 2004
  5. Krivenok Dmitry
    Replies:
    13
    Views:
    1,452
    Axter
    Jun 1, 2006
Loading...

Share This Page