Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reference variable assignment and copy constructor call

Reference variable assignment and copy constructor call

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testingquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    trinh nguyen
    wrote on last edited by
    #1

    I am testing the constructor call with the code below:

    #include class A
    {
    public:
    A(int n = 0)
    : m_n(n)
    {

    }
    
    A(const A& a)
        : m\_n(a.m\_n)
    {
    
    }
    

    private:
    int m_n;
    };

    int main()
    {
    A a(2), b(1);

    const A c(a);
    
    const A &d = c;
    
    const A e = b; //Note 1
    
    b = d; //Note 2: I am confused here
    
    return 0;
    

    }

    Everything is OK with me. But there is one point that I cannot understand is that: At the line "b = d" (Note 2), why the copy constructor is not called while at the line "const A e = b" (Note 2) the constructor is called? I expect that at line Note 2, the copy constructor will be called to copy the value of "c" (referenced by "d") to "b", but it doesn't happen. Please explain it, thank you in advance!

    L 1 Reply Last reply
    0
    • T trinh nguyen

      I am testing the constructor call with the code below:

      #include class A
      {
      public:
      A(int n = 0)
      : m_n(n)
      {

      }
      
      A(const A& a)
          : m\_n(a.m\_n)
      {
      
      }
      

      private:
      int m_n;
      };

      int main()
      {
      A a(2), b(1);

      const A c(a);
      
      const A &d = c;
      
      const A e = b; //Note 1
      
      b = d; //Note 2: I am confused here
      
      return 0;
      

      }

      Everything is OK with me. But there is one point that I cannot understand is that: At the line "b = d" (Note 2), why the copy constructor is not called while at the line "const A e = b" (Note 2) the constructor is called? I expect that at line Note 2, the copy constructor will be called to copy the value of "c" (referenced by "d") to "b", but it doesn't happen. Please explain it, thank you in advance!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi, You are not invoking the assignment operator with your const A e = b;. You are constructing a new object of const A e and performing initialization by copy of b. This is covered in section 8.5 paragraph 15 of the latest standard. www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf‎[^] Best Wishes, -David Delaune

      T 1 Reply Last reply
      0
      • L Lost User

        Hi, You are not invoking the assignment operator with your const A e = b;. You are constructing a new object of const A e and performing initialization by copy of b. This is covered in section 8.5 paragraph 15 of the latest standard. www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf‎[^] Best Wishes, -David Delaune

        T Offline
        T Offline
        trinh nguyen
        wrote on last edited by
        #3

        Thanks Randor for your prompt reply. As your answer, I can conclude that the copy constructor will be call for initialization only, not for "=" (assignment operator). Since long time ago, I have believed that the copy constructor will be call for "=" operator if there is no "=" operator defined for that class. But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy? Thanks.

        L 1 Reply Last reply
        0
        • T trinh nguyen

          Thanks Randor for your prompt reply. As your answer, I can conclude that the copy constructor will be call for initialization only, not for "=" (assignment operator). Since long time ago, I have believed that the copy constructor will be call for "=" operator if there is no "=" operator defined for that class. But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy? Thanks.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hi,

          trinh.nguyen wrote:

          But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy?

          If you do not write an assignment operator for your class the compiler will generate a default assignment operator that performs a memberwise copy[^] of base classes and members. Have a look at section 12.8 paragraph 15: www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf[^] Best Wishes, -David Delaune

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups