473,770 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static_cast confusion

Hi all,

Following is the assigment operator of a derived class

Derived& Derived::operat or=(const Derived& inDerived)
{
//to assign to the base class object the following statement

static_cast<Bas e&>(*this)=inDe rived;//works fine

static_cast<Bas e> (*this)=inDeriv ed;//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 7382
buds wrote:
Hi all,

Following is the assigment operator of a derived class

Derived& Derived::operat or=(const Derived& inDerived)
{
//to assign to the base class object the following statement

static_cast<Bas e&>(*this)=inDe rived;//works fine
This invokes the base class's assignment operator.
static_cast<Bas e> (*this)=inDeriv ed;//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_ca st<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_cas t that would be needed for the specific
conversion.

Jul 19 '05 #2
> Derived& Derived::operat or=(const Derived& inDerived)
{
//to assign to the base class object the following statement

static_cast<Bas e&>(*this)=inDe rived;//works fine

static_cast<Bas e> (*this)=inDeriv ed;//calls the base class copy constructor.
coz, static_cast<Bas e> results into creation of a temporary obj of type Base


*((Base*)this) = inDerived;//works fine
this is same as *(static_cast<B ase*>(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::operat or=(const Derived& inDerived)
{
//to assign to the base class object the following statement

static_cast<Bas e&>(*this)=inDe rived;//works fine

static_cast<Bas e> (*this)=inDeriv ed;//calls the base class copy
constructor.


coz, static_cast<Bas e> results into creation of a temporary obj of
type Base


*((Base*)this) = inDerived;//works fine


this is same as *(static_cast<B ase*>(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.co m> wrote in message news:31******** *************** ***@posting.goo gle.com...

static_cast<Bas e> (*this)=inDeriv ed;//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_ca st<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_ca st<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.co m> píse v diskusním príspevku
news:31******** *************** ***@posting.goo gle.com...
Hi all,

Following is the assigment operator of a derived class

Derived& Derived::operat or=(const Derived& inDerived)
{
//to assign to the base class object the following statement

static_cast<Bas e&>(*this)=inDe rived;//works fine

static_cast<Bas e> (*this)=inDeriv ed;//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
6366
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
5154
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 one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
9
4174
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 can usually get around the situation by inserting a "using A::..." statement inside class B, however, due to this being a static cast, what is the syntax?
26
2556
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 favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
2
1961
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.. template <class T> class Node{ protected: T e; Node* left;
19
3760
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 other.
24
4767
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
3913
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 static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
3
367
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; // Compilation error , OK i= static_cast<int *>(d); // Compilation error. OK
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.