Another 'Why & Why Not' Question
Code mentioned in a book fr showing how 2 do Assignment Op. O/Loading.
#include<iostream.h>
class alpha
{
int data;
public:
alpha(){}
alpha(int d)
{
data=d;
}
void display()
{
cout<<data;
}
alpha operator =(alpha& a)
{
data=a.data;
cout<<"Assignment op. invoked";
return alpha(data);
}
};
void main()
{
alpha a1(37);
alpha a2;
a2=a1; //invokes O/Loaded Ass. Op.
cout<<"\n a2";a2.display();
alpha a3=a2; // Does not invoke O/Loaded Ass. Op.
cout<<"\na3=";a3.display();
}
What I wanna know here is tt in the Operator O/Loading bit
alpha operator =(alpha& a)
{
data=a.data;
cout<<"Assignment op. invoked";
return alpha(data);
}
the guy has specified alpha as a return type...I mean why shud it be alpha...y cant it b just a void return type.....after all...we're only going 2 b doing
alpha a3=a2
so y isnt the following sufficient
void operator =(alpha& a)
{
data=a.data;
cout<<"Assignment op. invoked";
}