MyClass &MyClass::operator = (const MyClass &myClass)
{
a = myClass.a; // it's crash after this line
b = myClass.b;
return *this;
}
MyClass MyClass::getMyClass()
{
MyClass wantedObject;
return wantedObject;
}
MyClass a;
MyClass b;
b = a.getMyClass(); // Runtime error
// I don't want a = b; I just want another MyClass object from getMyClass to return.
getMyClass is a member function, so it either has to refer to an object instance (which it doesn't) or be called within another member function of MyClass (which you haven't shown).
As shown, getMyClass is going to construct a default instance and return it by value.
Compilers don't detect run time access violations. A run time access violation is something that happens AFTER your program is compiled when you execute your program.