C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static_cast confusion

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Chandra Shekhar Kumar
Guest





PostPosted: Tue Jun 24, 2003 10:49 pm    Post subject: Re: static_cast confusion Reply with quote



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





PostPosted: Wed Jun 25, 2003 10:36 am    Post subject: static_cast confusion Reply with 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

TIA
Buds
Back to top
Rolf Magnus
Guest





PostPosted: Wed Jun 25, 2003 10:39 am    Post subject: Re: static_cast confusion Reply with quote



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





PostPosted: Wed Jun 25, 2003 12:01 pm    Post subject: Re: static_cast confusion Reply with quote

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





PostPosted: Wed Jun 25, 2003 2:57 pm    Post subject: Re: static_cast confusion Reply with quote


"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





PostPosted: Wed Jun 25, 2003 3:00 pm    Post subject: Re: static_cast confusion Reply with quote


"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





PostPosted: Wed Jun 25, 2003 3:21 pm    Post subject: Re: static_cast confusion Reply with quote

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





PostPosted: Fri Jun 27, 2003 2:54 pm    Post subject: Re: static_cast confusion Reply with quote


"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.