C++ compiler will not provide default copy assignment operator to a class which has reference member(and some other scenarios also). Reason is, if default copy assignment operator is provided then both source and destination object's reference member refers to same copy.
However, default copy constructor is provided in same scenario and this introduces same problem as providing default copy assignment operator.
Any reason for providing default copy constructor?
#include <iostream>
using namespace std;
class People{
public:
People(string name = ""):m_name(name){
}
string getName(){
return m_name;
}
void setName(string name){
m_name = name;
}
private:
string& m_name;//reference member
};
int main() {
People a("Erik");
People b(a);
a.setName("Tom");
cout << a.getName() << endl;//This prints "Tom"
cout << b.getName() << endl;//This prints "Tom"
//a = b; //Build error
return 0;
}