I've looked around a bit on Google and at a few different articles but I'm pretty sure I'm not looking for the right information.
In the following scenario, I have a very basic assignment operator:
Code:
class MyClass
{
public:
MyClass(Bar *bar) : myBar(bar) {};
MyClass(): myBar(NULL) {};
MyClass& operator=(const MyClass &rhs)
Bar *myBar;
};
MyClass& MyClass::operator=(const MyClass &rhs)
{
myBar= rhs.myBar; // explodes here, access violation at 0x00000000
}
int main()
{
Bar *someBar = new Bar();
MyClass *myClass = new MyClass(someBar);
MyClass *myClass2 = new MyClass();
*myClass2 = *myClass;
return 0;
}
As a note, myBar is an object that is constructed outside of MyClass and is
What I expect is that the address of rhs.myBar be assigned to myBar. However, I get an access violation attempting to write to memory 0x00000000 which tells me that I'm trying to write into a null pointer.
My first thought was that I should be asking for the address of rhs.myBar like this:
Code:
myBar = &rhs.myBar;
But that only results in the error message "cannot convert from Bar *const *' to Bar*'". I'm pretty sure const casting is not applicable here and I'm pretty sure that my first thought is just plain wrong.
Obviously my code is incorrect. What am I doing wrong here? Where can I find more information about the particular type of operation I'm trying to do?