"Swap" baseclass of two similar Objects...

Discussion in 'C++' started by .rhavin grobert, Oct 7, 2007.

  1. 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;)
    .rhavin grobert, Oct 7, 2007
    #1
    1. Advertising

  2. 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
    Markus Schoder, Oct 7, 2007
    #2
    1. Advertising

  3. * 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.

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

    should then do the trick.

    But it's not a good idea.

    Class C may impose constraints on its B subobject, that depends on the
    additional information present, and so swapping may violate the
    constraints present in C and break C's class invariant. It's basically
    a slice operation. And slicing is Evil(TM) and should be avoided.

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 7, 2007
    #3
  4. 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
    Markus Schoder, Oct 7, 2007
    #4
  5. On 7 Okt., 18:53, "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.



    i meant class C: public A, B {...};

    before:

    C1 = [A1],[B1]
    C2 = [A2],[B2]

    after:

    C1 = [A1],[B2]
    C2 = [A2],[B1]

    even if B has private stuff.

    >
    > std::swap( *static_cast<B*>(C1), *static_cast<B*>(C2) );
    >
    > should then do the trick.
    >
    > But it's not a good idea.
    >
    > Class C may impose constraints on its B subobject, that depends on the
    > additional information present, and so swapping may violate the
    > constraints present in C and break C's class invariant.


    C and A are contolled by myself, so i should have a chance to correct
    the odds;-)

    > It's basically a slice operation. And slicing is Evil(TM) and should be avoided.


    I want to be evil ;-)

    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    .rhavin grobert, Oct 7, 2007
    #5
  6. ..rhavin grobert wrote:
    > On 7 Okt., 18:53, "Alf P. Steinbach" <> 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.
    Gianni Mariani, Oct 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. verbatime
    Replies:
    7
    Views:
    345
    John Harrison
    Mar 4, 2004
  2. Steven T. Hatton
    Replies:
    2
    Views:
    414
    tom_usenet
    Aug 16, 2004
  3. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    988
    Niels Dekker (no reply address)
    Jul 20, 2006
  4. shaun roe
    Replies:
    6
    Views:
    340
    mlimber
    Jan 19, 2007
  5. Juha Nieminen
    Replies:
    16
    Views:
    523
    Kai-Uwe Bux
    Sep 17, 2007
Loading...

Share This Page