assignment operator and copy constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    assignment operator and copy constructor

    class A
    {
    public:
    A() {cout << "1";}
    A operator=(const A&){cout << "2"; return *this;}
    A (const A&) {cout << "3";}
    };
    int main()
    {
    A a1;
    A a2 = a1;//line 1
    a2 = a1;//line 2
    }

    pls explain which functions will be called in line 1 and line 2 and why??
  • mush1578
    New Member
    • Feb 2010
    • 15

    #2
    in line a2 is the object of class A and a1 ia the refrence variable
    this is used in inhitence class
    in which we call all the function of class without creating there obj and call these by refrence variable

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Think about it. In Line 1 the object a2 does not exist. Therefore, a constructor will be called. In line 2 the object a2 does exist so an assignment operator will be called.

      This question is in every job interview I have ever taken.

      Comment

      Working...