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 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.
> 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..
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.
"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.
"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.
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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
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...
|
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...
|
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...
|
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..
...
|
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...
|
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);
|
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...
|
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; ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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,...
| |