Thread: A Static_cast<> Question

  1. #1
    Registered User
    Join Date
    Apr 2007

    A Static_cast<> Question


    class Derived: public Base{
    what is the difference between:

    Derived * d = new Derived;
    Base* b = static_cast<Base*>(d);

    Derived  d ;
    Base b = static_cast<Base>(d);
    Last edited by meili100; 06-07-2007 at 12:07 PM.

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Buffalo, NY
    Polymorphic relationships don't hold for actual objects.

    That is:

    A Derived* is a Base*
    A Derived& is a Base&

    But a Derived is NOT a Base. Such a conversion results in slicing, and you lose pieces of your objects.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  3. #3
    Registered User
    Join Date
    Jan 2005
    The second one makes a copy of the object and casts it (which is where the slicing happens). The first one makes a copy of the pointer and casts it, so the object is still there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM