0

I encountered this code but I could not understand the functionality of this code. It would be a great help if someone could explain it .

struct A{
   int i,j;
   A(int ii,int jj) : i(ii),j(ii){}

   A(const A&a){
           }
   A& operator =(const A& a){
               i=a.i;j=a.j;
   }
};

int main()
{
int i;
A a(1,2);
A b(2,3);
A z = (a=b);
cout<<z.i<<" "<<z.j<<endl;

system("pause");
return 0;
}
4
  • 3
    What part do you not understand? Commented Apr 21, 2012 at 4:03
  • the operator overloadin part. if we take a as const how can we edit it. Commented Apr 21, 2012 at 4:04
  • Overload the default constructor: java2s.com/Tutorial/Cpp/0180__Class/Overloadtheconstructor.htm Commented Apr 21, 2012 at 4:06
  • maybe edit the post to reflect that? You only need to show about two lines of code and ask a very specific question. Commented Apr 21, 2012 at 5:10

4 Answers 4

1

Explanation:

struct A{
   int i,j;//members i and j

   A(int ii,int jj) : i(ii),j(ii){} //A constructor. Short form of A(int ii,int jj){i = ii;j = jj;} Original code is wrong too. Should be j(jj) instead of j(ii)

   A(const A&a){}//Another constructor. It is missing the assignment

   A& operator =(const A& a){
               i=a.i;j=a.j;
   }//Equal operator overload definition for A = another A. It copies the data from another A and assign to this new one
};

Complete working code:

#include <iostream>
using namespace std;

struct A{
   int i,j;

   A(int ii,int jj) : i(ii),j(jj){}

   A(const A&a){i=a.i;j=a.j;}

   A& operator =(const A& a){i=a.i;j=a.j;}
};

int main()
{
    int i;
    A a(1,2);
    A b(2,3);
    A z = (a=b);
    cout<<z.i<<" "<<z.j<<endl;

    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

why does "z" receive a garbage value?
I don't know what your code is used for so cannot comment on if it is garbage. But z=(a=b) simply assign a = b, and assign z = a (or b because they are now the same)
Because the assignment isn't defined in the constructor
z receives a garbage value because it calls the Copy Constructor that you left empty, not the operator= method that you defined.
Oh you mean that...yes. A z=a calls A(const A& a) instead of = operator; the reason is unknown but this is the rule. But if you do A z(0,0); z = a;, then it will call =.
1

Your problem is this line:

A z = (a=b);

It ends up invoking both your operator= method and your Copy Constructor. When a = b is executed, it uses the operator= method because a already exists and then a reference to a is returned. You're essentially calling a.operator=(b).

When A z = ... is executed, it actually uses the Copy Constructor A(const A&a), not the operator= method because z does not exist yet. Since z is being created by that copy constructor and i and j are never initialized, when you try to print them out, you get whatever junk was located in the memory reserved for i and j.

Another way to view this line:

A z = (a=b);

Is actually like this:

A z(a.operator=(b));

Here's a full example:

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

    a = b; //calls A& operator=(const A& a)

    A z = a; //calls A(const A& a)
}

In conclusion, the fix is to do this:

A(const A& a)
   {
        i = a.i;
        j = a.j;
   }

Comments

0

Three mistakes:

1.A(int ii,int jj) : i(ii),j(ii /* jj here? */){}

2.The copy constructor should initialize the members: A(const A&a): i(a.i), j(a.j) {}

3.You should Add return *this in operator=:

A& operator =(const A& a){
    i=a.i;j=a.j;
    return *this;
}

Comments

-1

The OP has asked the operator overloadin part. if we take a as const how can we edit it.

The operator overloading part is:

A& operator =(const A& a){
           i=a.i;j=a.j;

}

When you write a=b, you only expect a to change and not b. This is enforced by the const specifier in the function argument definition. This const specifier has nothing to do with whatever appears on the left hand side of the equal sign. It only says that the right hand side of the equal sign will not be modified.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.