Class Assignment Operators
Posted
by
Maxpm
on Stack Overflow
See other posts from Stack Overflow
or by Maxpm
Published on 2010-12-22T13:41:55Z
Indexed on
2010/12/22
13:54 UTC
Read the original article
Hit count: 271
I made the following operator overloading test:
#include <iostream>
#include <string>
using namespace std;
class TestClass
{
string ClassName;
public:
TestClass(string Name)
{
ClassName = Name;
cout << ClassName << " constructed." << endl;
}
~TestClass()
{
cout << ClassName << " destructed." << endl;
}
void operator=(TestClass Other)
{
cout << ClassName << " in operator=" << endl;
cout << "The address of the other class is " << &Other << "." << endl;
}
};
int main()
{
TestClass FirstInstance("FirstInstance");
TestClass SecondInstance("SecondInstance");
FirstInstance = SecondInstance;
SecondInstance = FirstInstance;
return 0;
}
The assignment operator behaves as-expected, outputting the address of the other class.
Now, how would I actually assign something from the other class? For example, something like this:
void operator=(TestClass Other)
{
ClassName = Other.ClassName;
}
© Stack Overflow or respective owner