copy constructor question

Discussion in 'C++' started by Ilia Poliakov, Aug 25, 2003.

  1. #include <iostream>

    using namespace std;

    class B
    {
    public:
    B() {};
    B(const B&)
    {
    cout << "Constructor called for B" << endl;
    };
    ~B() {};
    };

    struct A
    {
    B m_oB;

    public:
    A(B& rB)
    {
    cout << "test1" << endl;
    m_oB = rB;

    cout << "test2" << endl;

    B oTestB = rB;
    };

    ~A() {};
    };

    void main()
    {
    B b;
    A a(b);
    }

    Output:

    test1
    test2
    Constructor called for B

    Why was not B::B(const B&) constructor called in test1 case?

    PS. I work under MSVC7.0
     
    Ilia Poliakov, Aug 25, 2003
    #1
    1. Advertising

  2. Ilia Poliakov

    Ron Natalie Guest

    "Ilia Poliakov" <> wrote in message news:bidd4d$7kq1h$-berlin.de...
    >> A(B& rB)

    > {
    > cout << "test1" << endl;
    > m_oB = rB;


    This isn't construction it's assignment!

    > void main()
    > {

    Main must return int!


    > Why was not B::B(const B&) constructor called in test1 case?
    >

    Because you don't construct a B object from another B object.
    You construct an A object from a B. That constructor does
    an assigment of B to B which calls the implicitly-generated
    copy-assignment operator after m_oB was derfault constructed.

    If you want to copy construct the member in the other constructor
    you need to do this.

    A(B& rB) : m_oB(rB) {
    }
     
    Ron Natalie, Aug 25, 2003
    #2
    1. Advertising

  3. Ilia Poliakov

    Kevin Saff Guest

    "Ilia Poliakov" <> wrote in message
    news:bidd4d$7kq1h$-berlin.de...
    > struct A
    > {
    > B m_oB;
    >
    > public:
    > A(B& rB)
    > {
    > cout << "test1" << endl;
    > m_oB = rB;


    // This calls operator=, not a constructor.

    >
    > cout << "test2" << endl;
    >
    > B oTestB = rB;


    // This calls the copy constructor, not operator=.

    > };
    >
    > ~A() {};
    > };
    >
    > void main()
    > {
    > B b;
    > A a(b);
    > }
    >
    > Output:
    >
    > test1
    > test2
    > Constructor called for B
    >
    > Why was not B::B(const B&) constructor called in test1 case?


    T newT = oldT;

    // actually calls:

    T newT(oldT);

    // if available. However,

    T newT;
    newT = oldT;

    // will call operator=.

    // You should define operator= to do what you want.
     
    Kevin Saff, Aug 25, 2003
    #3
  4. Ilia Poliakov wrote:

    > #include <iostream>
    >
    > using namespace std;
    >
    > class B
    > {
    > public:
    > B() {};


    Get rid of these semi-colons at the end of functions.

    > B(const B&)
    > {
    > cout << "Constructor called for B" << endl;
    > };
    > ~B() {};
    > };
    >
    > struct A
    > {
    > B m_oB;
    >
    > public:
    > A(B& rB)
    > {
    > cout << "test1" << endl;
    > m_oB = rB;


    Copy assignment. No problem.

    >
    > cout << "test2" << endl;
    >
    > B oTestB = rB;


    Copy construction. No problem.

    > };
    >
    > ~A() {};
    > };
    >
    > void main()


    void is not and never has been an acceptable return type for main. main
    has always been required to return int.

    > {
    > B b;
    > A a(b);
    > }
    >
    > Output:
    >
    > test1
    > test2
    > Constructor called for B
    >
    > Why was not B::B(const B&) constructor called in test1 case?


    It's not clear why you believe it should have been. No construction
    occurred there. Only an assignment to an existing object.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 26, 2003
    #4
  5. Ilia Poliakov

    Liu Jian Guest

    "Ilia Poliakov" <> wrote in message
    news:bidd4d$7kq1h$-berlin.de...
    > #include <iostream>
    >
    > using namespace std;
    >
    > class B
    > {
    > public:
    > B() {};
    > B(const B&)
    > {
    > cout << "Constructor called for B" << endl;
    > };
    > ~B() {};
    > };
    >
    > struct A
    > {
    > B m_oB;
    >
    > public:
    > A(B& rB)
    > {
    > cout << "test1" << endl;
    > m_oB = rB;


    // Change it like following calls copy constractor of B
    // B oB = rB;
    // or B oB(rB);


    >
    > cout << "test2" << endl;
    >
    > B oTestB = rB;
    > };
    >
    > ~A() {};
    > };
    >
    > void main()
    > {
    > B b;
    > A a(b);
    > }
    >
    > Output:
    >
    > test1
    > test2
    > Constructor called for B
    >
    > Why was not B::B(const B&) constructor called in test1 case?
    >
    > PS. I work under MSVC7.0
    >
    >
     
    Liu Jian, Aug 26, 2003
    #5
    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. VisionSet
    Replies:
    8
    Views:
    4,902
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    469
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    579
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,248
  5. cinsk
    Replies:
    35
    Views:
    2,615
    James Kanze
    Oct 11, 2010
Loading...

Share This Page