473,396 Members | 1,892 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.

"Swap" baseclass of two similar Objects...

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
3 1977
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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
2
by: Eric Lilja | last post by:
...during the call to swap_arrays the name of the array decays to a pointer to its first element and that pointer is a temporary variable and you cant bind a non-const reference to a temporary....
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
3
by: groovymovin | last post by:
Hi there, I am a newbee. searched intensively for a quite simple problem but couldnt find an answer. Here is my problem: I have two layers with 2 movie clips. The movie clip on layer 1 stays...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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.