I have made a simple class which overloads an equals operator. So far, it does nothing important, becuase its just a test.
The class just looks like this:
Code:
class myoverloadAttempt
{
public:
int& val;
myoverloadAttempt(int& x) : val(x) {};
myoverloadAttempt& operator=(const int& x)
{
val=x;
return *this;
};
};
and here is how I use it:
Code:
int myvar = 2;
myoverloadAttempt o(myvar);
o = 5;
//myoverloadAttempt* o = new myoverloadAttempt(myvar);
//o->operator =(5);
std::cout<<myvar<<"\n";
It works fine with those 2 lines commented out, ie using the class not as a pointer, but when I switch over and try to use it where o is a pointer to this class, o=5 no longer works. It says "error C2440: '=' : cannot convert from 'int' to 'myoverloadAttempt *'"
When o is a pointer, It only worked when I used the line, o->operator=(5)
.
Is this always the case? Is there no way to use overloaded operators when dealing with a pointer?