By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

"Swap" baseclass of two similar Objects...

P: n/a
guess you have two classes (A and B) and you have two objects (C1 and
C2) of a class C that is defined as

class C: public A, public B

can I *somehow* swap B of C1 and B of C2? To make myself clearer:
class C1 and C2 both have some area in memory that holds their B-Part
- i guess there must be somewhere in class C1 and C2 a kind of pointer
to that baseclasses. Eg. if you do a (B*) this inside C1 you get it.
What i want to do is swap the pointers.

Is that somehow possible?

thx in advance, .rhavin;)

Oct 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 07 Oct 2007 09:05:40 -0700, .rhavin grobert wrote:
guess you have two classes (A and B) and you have two objects (C1 and
C2) of a class C that is defined as

class C: public A, public B

can I *somehow* swap B of C1 and B of C2? To make myself clearer: class
C1 and C2 both have some area in memory that holds their B-Part - i
guess there must be somewhere in class C1 and C2 a kind of pointer to
that baseclasses. Eg. if you do a (B*) this inside C1 you get it. What i
want to do is swap the pointers.

Is that somehow possible?
Most implementations will not hold pointers to the base class objects but
instead aggregate them into one large object -- therefore it is not even
theoretically possible to do what you want.

--
Markus Schoder
Oct 7 '07 #2

P: n/a
On Sun, 07 Oct 2007 18:53:42 +0200, Alf P. Steinbach wrote:
* Markus Schoder:
>On Sun, 07 Oct 2007 09:05:40 -0700, .rhavin grobert wrote:
>>guess you have two classes (A and B) and you have two objects (C1 and
C2) of a class C that is defined as

class C: public A, public B

can I *somehow* swap B of C1 and B of C2? To make myself clearer:
class C1 and C2 both have some area in memory that holds their B-Part
- i guess there must be somewhere in class C1 and C2 a kind of pointer
to that baseclasses. Eg. if you do a (B*) this inside C1 you get it.
What i want to do is swap the pointers.

Is that somehow possible?

Most implementations will not hold pointers to the base class objects
but instead aggregate them into one large object -- therefore it is not
even theoretically possible to do what you want.

I think perhaps you misread what the OP wrote although it's not very
clear when he first refers to C1 as an object and then as a class; I'm
assuming C1 and C2 really are objects, of class C.
Only the OP would now. I thought the OP wanted to specifically swap
pointers to the base class objects and not the actual content since it
seems obvious that you can swap the content.
std::swap( *static_cast<B*>(C1), *static_cast<B*>(C2) );
should probably read

std::swap( *static_cast<B*>(&C1), *static_cast<B*>(&C2) );

--
Markus Schoder
Oct 7 '07 #3

P: n/a
..rhavin grobert wrote:
On 7 Okt., 18:53, "Alf P. Steinbach" <al...@start.nowrote:
....
>It's basically a slice operation. And slicing is Evil(TM) and should be avoided.

I want to be evil ;-)
I think Alf really meant to say masochistic.
Oct 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.