static_cast confusion

Discussion in 'C++' started by Chandra Shekhar Kumar, Jun 24, 2003.

  1. > Derived& Derived::eek:perator=(const Derived& inDerived)
    > {
    > //to assign to the base class object the following statement
    >
    > static_cast<Base&>(*this)=inDerived;//works fine
    >
    > static_cast<Base> (*this)=inDerived;//calls the base class copy constructor.


    coz, static_cast<Base> results into creation of a temporary obj of type Base

    >
    >
    > *((Base*)this) = inDerived;//works fine


    this is same as *(static_cast<Base*>(this)) = inDerived;

    >
    >
    > return (*this);
    > }
    >
    > I thought the static_cast was equivalent to the C-style cast.


    yes, u r right, see the above. but ideally u shud use dynamic_cast for
    polymorphic classes..
     
    Chandra Shekhar Kumar, Jun 24, 2003
    #1
    1. Advertising

  2. Chandra Shekhar Kumar

    buds Guest

    Hi all,

    Following is the assigment operator of a derived class

    Derived& Derived::eek:perator=(const Derived& inDerived)
    {
    //to assign to the base class object the following statement

    static_cast<Base&>(*this)=inDerived;//works fine

    static_cast<Base> (*this)=inDerived;//calls the base class copy constructor.

    *((Base*)this) = inDerived;//works fine

    return (*this);
    }

    I thought the static_cast was equivalent to the C-style cast.
    Then why this behavior. Can anyone please help me with this

    TIA
    Buds
     
    buds, Jun 25, 2003
    #2
    1. Advertising

  3. Chandra Shekhar Kumar

    Rolf Magnus Guest

    buds wrote:

    > Hi all,
    >
    > Following is the assigment operator of a derived class
    >
    > Derived& Derived::eek:perator=(const Derived& inDerived)
    > {
    > //to assign to the base class object the following statement
    >
    > static_cast<Base&>(*this)=inDerived;//works fine


    This invokes the base class's assignment operator.

    > static_cast<Base> (*this)=inDerived;//calls the base class copy
    > constructor.


    Right.

    >
    > *((Base*)this) = inDerived;//works fine


    With this, you tell the compiler "hey, I know that 'this' is a pointer
    to Derived, but that's not really true. Actually it's a Base, so treat
    the pointer as if it were a Base.". It's equivalent to:

    *reinterpret_cast<Base*>(this) = inDerived;//works fine

    Don't expect that to work generally.

    >
    > return (*this);
    > }
    >
    > I thought the static_cast was equivalent to the C-style cast.


    No. The C style cast is equivalent to any combination of static_cast,
    const_cast and reinterpret_cast that would be needed for the specific
    conversion.
     
    Rolf Magnus, Jun 25, 2003
    #3
  4. Chandra Shekhar Kumar

    Rolf Magnus Guest

    Chandra Shekhar Kumar wrote:

    >> Derived& Derived::eek:perator=(const Derived& inDerived)
    >> {
    >> //to assign to the base class object the following statement
    >>
    >> static_cast<Base&>(*this)=inDerived;//works fine
    >>
    >> static_cast<Base> (*this)=inDerived;//calls the base class copy
    >> constructor.

    >
    > coz, static_cast<Base> results into creation of a temporary obj of
    > type Base
    >
    >>
    >>
    >> *((Base*)this) = inDerived;//works fine

    >
    > this is same as *(static_cast<Base*>(this)) = inDerived;


    Hmm. I guess I was wrong then.

    >> return (*this);
    >> }
    >>
    >> I thought the static_cast was equivalent to the C-style cast.

    >
    > yes, u r right, see the above. but ideally u shud use dynamic_cast for
    > polymorphic classes..


    Why? You need dynamic_cast to cast from base to derived, not the other
    way round.
     
    Rolf Magnus, Jun 25, 2003
    #4
  5. Chandra Shekhar Kumar

    Ron Natalie Guest

    "buds" <> wrote in message news:...


    > static_cast<Base> (*this)=inDerived;//calls the base class copy constructor.


    >
    > I thought the static_cast was equivalent to the C-style cast.


    You are wrong. Some C style casts are NOT the same as static_cast (but it's
    immaterial here).

    > Then why this behavior. Can anyone please help me with this


    The base class copy constructor is called because in order to cast Derived
    to Base, a temporary Base object is created and that is what is assigned into.
     
    Ron Natalie, Jun 25, 2003
    #5
  6. Chandra Shekhar Kumar

    Ron Natalie Guest

    "Rolf Magnus" <> wrote in message news:bdbuec$lod$04$-online.com...
    \
    >
    > With this, you tell the compiler "hey, I know that 'this' is a pointer
    > to Derived, but that's not really true. Actually it's a Base, so treat
    > the pointer as if it were a Base.". It's equivalent to:
    >
    > *reinterpret_cast<Base*>(this) = inDerived;//works fine
    >
    > Don't expect that to work generally.
    >

    Huh? The C cast should do a static cast here. The cast works generally
    provided that Base is a public base class of Derived.
     
    Ron Natalie, Jun 25, 2003
    #6
  7. Chandra Shekhar Kumar

    Rolf Magnus Guest

    Ron Natalie wrote:

    >
    > "Rolf Magnus" <> wrote in message
    > news:bdbuec$lod$04$-online.com... \
    >>
    >> With this, you tell the compiler "hey, I know that 'this' is a
    >> pointer to Derived, but that's not really true. Actually it's a Base,
    >> so treat the pointer as if it were a Base.". It's equivalent to:
    >>
    >> *reinterpret_cast<Base*>(this) = inDerived;//works fine
    >>
    >> Don't expect that to work generally.
    >>

    > Huh? The C cast should do a static cast here. The cast works
    > generally provided that Base is a public base class of Derived.


    Yes, I think you're right. Sorry.
     
    Rolf Magnus, Jun 25, 2003
    #7
  8. Chandra Shekhar Kumar

    Mirek Fidler Guest

    "buds" <> píse v diskusním príspevku
    news:...
    > Hi all,
    >
    > Following is the assigment operator of a derived class
    >
    > Derived& Derived::eek:perator=(const Derived& inDerived)
    > {
    > //to assign to the base class object the following statement
    >
    > static_cast<Base&>(*this)=inDerived;//works fine
    >
    > static_cast<Base> (*this)=inDerived;//calls the base class copy

    constructor.
    >
    > *((Base*)this) = inDerived;//works fine
    >
    > return (*this);
    > }
    >
    > I thought the static_cast was equivalent to the C-style cast.
    > Then why this behavior. Can anyone please help me with this


    It is. Problem is that second static_cast transforms into

    (Base)(*this) = inDerived;

    which, following rules for casting is equivalent for

    Base(*this) = inDerived;

    which means "create temporary object of Base type using Base(const
    Base&) copy constructor and use its operator= .

    Mirek
     
    Mirek Fidler, Jun 27, 2003
    #8
    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. Wenjie
    Replies:
    4
    Views:
    435
    Victor Bazarov
    Jul 12, 2003
  2. alg

    static_cast<>

    alg, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    505
  3. Alan Sung

    Re: static_cast question

    Alan Sung, Aug 2, 2003, in forum: C++
    Replies:
    0
    Views:
    521
    Alan Sung
    Aug 2, 2003
  4. Bo Peng
    Replies:
    11
    Views:
    1,118
    Victor Bazarov
    Oct 20, 2006
  5. junyangzou
    Replies:
    13
    Views:
    289
Loading...

Share This Page