#include<iostream>
using namespace std;
class alpha{
private:
int data;
public:
alpha(){}
alpha(int a){data=a;}
void display(){
cout<<data;
}
alpha operator =(alpha& a){
cout<<data;
data=a.data;
cout<<"\n assignement operator invoked";
return alpha(data); // is temporary object created is returned to a2 object ?//why it can't be returned by reference?what is the difference b/w assignement and initiallizing
}
};
int main(){
alpha a1(123);
alpha a2(10);
a2=a1;
cout<<"\na2";a2.display();
a1.display();
return 0;
}
saransh60 0
Newbie Poster
Recommended Answers
Jump to Post>is temporary object created is returned to a2 object ?
Conceptually, yes. In practice compilers can often optimize away return value temporaries.>why it can't be returned by reference?
Well, in this case it can't because then you would be returning a reference to a local object, which is …
All 3 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.