static_cast confusion

  • Thread starter Chandra Shekhar Kumar
  • Start date
C

Chandra Shekhar Kumar

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 said:
*((Base*)this) = inDerived;//works fine

this is same as *(static_cast said:
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..
 
B

buds

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
 
R

Rolf Magnus

buds said:
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.
 
R

Rolf Magnus

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

Hmm. I guess I was wrong then.
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.
 
R

Ron Natalie

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.
 
R

Ron Natalie

Rolf Magnus said:
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.
 
R

Rolf Magnus

Ron said:
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.
 
M

Mirek Fidler

buds said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top