473,788 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 2001
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.no wrote:
....
>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
3261
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 happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
8
2058
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 to use elsewhere. For example, suppose you have a class Note. This class stores some text, as a linked list of lines of text. The destructor runs as follows: Note::~Note() {
2
3565
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. Correct analysis? The code (that only compiles if you comment out the call to swap_arrays()): #include <iostream> #include <iterator> using namespace std;
28
2857
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 apply. Can somebody tell me why? Thanks, Jess
3
4275
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 static, the movie clip on layer 2 fades in on a few frames, thus I crossfade from clip 1 to clip 2 (actually it is a bit more complicated what I am trying to do). At the end of the fade, i would like to replace the movie clip on layer 1 with the movie...
19
10763
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 there an existing adapter? Thanks, Daniel.
0
9656
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
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10175
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10112
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
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
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.