Piyush Goyal
Piyush Goyal

Reputation: 101

Stucture in C++

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;
}

Upvotes: 0

Views: 237

Answers (4)

suresh
suresh

Reputation: 1109

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.

Upvotes: -1

Cosyn
Cosyn

Reputation: 5007

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;
}

Upvotes: 0

Brent Writes Code
Brent Writes Code

Reputation: 19623

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;
   }

Upvotes: 1

SwiftMango
SwiftMango

Reputation: 15294

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;
}

Upvotes: 1

Related Questions