copy constructor question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ilia Poliakov

    copy constructor question

    #include <iostream>

    using namespace std;

    class B
    {
    public:
    B() {};
    B(const B&)
    {
    cout << "Constructo r 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


  • Ron Natalie

    #2
    Re: copy constructor question


    "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message news:bidd4d$7kq 1h$1@ID-120622.news.uni-berlin.de...[color=blue][color=green]
    >> A(B& rB)[/color]
    > {
    > cout << "test1" << endl;
    > m_oB = rB;[/color]

    This isn't construction it's assignment!
    [color=blue]
    > void main()
    > {[/color]
    Main must return int!

    [color=blue]
    > Why was not B::B(const B&) constructor called in test1 case?
    >[/color]
    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) {
    }


    Comment

    • Kevin Saff

      #3
      Re: copy constructor question


      "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message
      news:bidd4d$7kq 1h$1@ID-120622.news.uni-berlin.de...[color=blue]
      > struct A
      > {
      > B m_oB;
      >
      > public:
      > A(B& rB)
      > {
      > cout << "test1" << endl;
      > m_oB = rB;[/color]

      // This calls operator=, not a constructor.
      [color=blue]
      >
      > cout << "test2" << endl;
      >
      > B oTestB = rB;[/color]

      // This calls the copy constructor, not operator=.
      [color=blue]
      > };
      >
      > ~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?[/color]

      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.



      Comment

      • Liu Jian

        #4
        Re: copy constructor question


        "Ilia Poliakov" <ipoliak@allesk lar.de> wrote in message
        news:bidd4d$7kq 1h$1@ID-120622.news.uni-berlin.de...[color=blue]
        > #include <iostream>
        >
        > using namespace std;
        >
        > class B
        > {
        > public:
        > B() {};
        > B(const B&)
        > {
        > cout << "Constructo r called for B" << endl;
        > };
        > ~B() {};
        > };
        >
        > struct A
        > {
        > B m_oB;
        >
        > public:
        > A(B& rB)
        > {
        > cout << "test1" << endl;
        > m_oB = rB;[/color]

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

        [color=blue]
        >
        > 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
        >
        >[/color]


        Comment

        • Kevin Goodsell

          #5
          Re: copy constructor question

          Ilia Poliakov wrote:
          [color=blue]
          > #include <iostream>
          >
          > using namespace std;
          >
          > class B
          > {
          > public:
          > B() {};[/color]

          Get rid of these semi-colons at the end of functions.
          [color=blue]
          > B(const B&)
          > {
          > cout << "Constructo r called for B" << endl;
          > };
          > ~B() {};
          > };
          >
          > struct A
          > {
          > B m_oB;
          >
          > public:
          > A(B& rB)
          > {
          > cout << "test1" << endl;
          > m_oB = rB;[/color]

          Copy assignment. No problem.
          [color=blue]
          >
          > cout << "test2" << endl;
          >
          > B oTestB = rB;[/color]

          Copy construction. No problem.
          [color=blue]
          > };
          >
          > ~A() {};
          > };
          >
          > void main()[/color]

          void is not and never has been an acceptable return type for main. main
          has always been required to return int.
          [color=blue]
          > {
          > B b;
          > A a(b);
          > }
          >
          > Output:
          >
          > test1
          > test2
          > Constructor called for B
          >
          > Why was not B::B(const B&) constructor called in test1 case?[/color]

          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.

          Comment

          Working...