Default copy operator on derivated classes

Discussion in 'C++' started by Alexander Tumarov, May 1, 2004.

  1. I tried to run next code compiles with g++

    //--------------------------------
    #include <stdio.h>

    class B
    {
    public:
    B& operator=(const B &b)
    {
    printf("BBBBBBBB \n");
    return *this;
    };
    };

    class C
    {
    public:
    C& operator=(const C &c)
    {
    printf("CCCCCCC \n");
    return *this;
    };
    };

    class D : public B,public C
    {
    public:
    D& operator=(const C &c)
    {
    printf("DDDDDD \n");
    return *this;
    };
    };



    int main(int argc, char* argv[])
    {
    C c1;
    D d1,d2;

    d1=d2;
    d1=c1;

    return 0;
    }

    // --------------------------

    And got next output

    BBBBBBBB
    CCCCCCC
    DDDDDD

    The question is why compiler builds call to both
    B& operator=(const B &b)
    C& operator=(const C &c)

    on the line
    d1=d2;
    ???

    A much as I remember on the case of missing copy operator the compiler
    should build default one that performs bit-to-bit copy and not call base
    copy operators. Am I wrong?
    Can anybody point me to the specific paragraph in the ANSI standard?

    Thank you.
     
    Alexander Tumarov, May 1, 2004
    #1
    1. Advertising

  2. Alexander Tumarov wrote in news:3ca86be5.0405010219.4ceceb05
    @posting.google.com in comp.lang.c++:

    >
    > The question is why compiler builds call to both
    > B& operator=(const B &b)
    > C& operator=(const C &c)
    >
    > on the line
    > d1=d2;
    > ???
    >
    > A much as I remember on the case of missing copy operator the compiler
    > should build default one that performs bit-to-bit copy and not call base
    > copy operators. Am I wrong?


    Yes you're wrong, the compiler generated D::eek:perator=(D const &) does
    member (and basewise) assignment using the members/bases (possibly
    compiler generated) operator=. The compiler never does bit-to-bit
    copying of a UDT, except perhaps under the as-if rule, when all
    members are builtins and use (or can use) bit-to-bit copying.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, May 1, 2004
    #2
    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. Replies:
    4
    Views:
    494
    Howard
    May 26, 2005
  2. Lars Uffmann
    Replies:
    13
    Views:
    1,034
    Lars Uffmann
    Feb 19, 2008
  3. puzzlecracker
    Replies:
    8
    Views:
    446
    James Kanze
    Apr 15, 2008
  4. Ornette

    Property persistence in derivated control

    Ornette, Sep 15, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    135
    Ornette
    Sep 15, 2008
  5. Mathieu Courtois

    Why derivated exception can not be pickled ?

    Mathieu Courtois, Sep 4, 2012, in forum: Python
    Replies:
    5
    Views:
    199
    Mathieu Courtois
    Sep 5, 2012
Loading...

Share This Page