473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 19 '05 #1
7 7345
buds wrote:
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.
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.

Jul 19 '05 #2
> 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;


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..
Jul 19 '05 #3
Chandra Shekhar Kumar wrote:
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.
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.

Jul 19 '05 #4

"buds" <bu**@ziplip.com> wrote in message news:31**************************@posting.google.c om...

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.
Jul 19 '05 #5

"Rolf Magnus" <ra******@t-online.de> wrote in message news:bd*************@news.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.
Jul 19 '05 #6
Ron Natalie wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bd*************@news.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.

Jul 19 '05 #7

"buds" <bu**@ziplip.com> píse v diskusním príspevku
news:31**************************@posting.google.c om...
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
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gary Labowitz | last post by:
Am I doing this correctly? It is a sample program for my class. #include <iostream> using namespace std; int main( ) { int x=3, y=4;
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
9
by: news.ir.com.au | last post by:
Hi, In the following code I get the compiler error: error C2243: 'static_cast' : conversion from 'class B *' to 'class A *' exists, but is inaccessible I understand why I get this error and...
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
2
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. ...
19
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the...
24
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
3
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.