| View previous topic :: View next topic |
| Author |
Message |
Chandra Shekhar Kumar Guest
|
Posted: Tue Jun 24, 2003 10:49 pm Post subject: Re: static_cast confusion |
|
|
| Quote: | Derived& Derived::operator=(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
| Quote: |
*((Base*)this) = inDerived;//works fine
|
this is same as *(static_cast<Base*>(this)) = inDerived;
| Quote: |
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..
|
|
| Back to top |
|
 |
buds Guest
|
Posted: Wed Jun 25, 2003 10:36 am Post subject: static_cast confusion |
|
|
Hi all,
Following is the assigment operator of a derived class
Derived& Derived::operator=(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
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Jun 25, 2003 10:39 am Post subject: Re: static_cast confusion |
|
|
buds wrote:
| Quote: | Hi all,
Following is the assigment operator of a derived class
Derived& Derived::operator=(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.
| Quote: | static_cast<Base> (*this)=inDerived;//calls the base class copy
constructor.
|
Right.
| Quote: |
*((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.
| Quote: |
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.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Jun 25, 2003 12:01 pm Post subject: Re: static_cast confusion |
|
|
Chandra Shekhar Kumar wrote:
| Quote: | Derived& Derived::operator=(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.
| Quote: | 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.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Jun 25, 2003 2:57 pm Post subject: Re: static_cast confusion |
|
|
"buds" <buds (AT) ziplip (DOT) com> wrote
| Quote: | 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).
| Quote: | 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.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Jun 25, 2003 3:00 pm Post subject: Re: static_cast confusion |
|
|
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
| Quote: |
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.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Jun 25, 2003 3:21 pm Post subject: Re: static_cast confusion |
|
|
Ron Natalie wrote:
| Quote: |
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote in message
news:bdbuec$lod$04$1 (AT) news (DOT) t-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.
|
|
| Back to top |
|
 |
Mirek Fidler Guest
|
Posted: Fri Jun 27, 2003 2:54 pm Post subject: Re: static_cast confusion |
|
|
"buds" <buds (AT) ziplip (DOT) com> píse v diskusním príspevku
news:31a7f443.0306250236.14381a92 (AT) posting (DOT) google.com...
| Quote: | Hi all,
Following is the assigment operator of a derived class
Derived& Derived::operator=(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
|
|
| Back to top |
|
 |
|