I want to overload the assignment operator for types like "int", "long" etc. That is, I want to use code like:
class CX {
private:
int data;
...
};
CX obj;
int k;
k = obj; // k should get the value of obj.data
Apparently assignment operators cannot be friend functions. How do I achieve the above?
I maybe missing something simple but just cant figure out the syntax/method to do this.
Also, one IMP restriction - we CANNOT use get/set methods because :: In release code, we will have CX typedefed as int or long (as required), but in DEBUG code, we want to have it as a class (for automatic type checking in thousands of places). The code needs to be common. Reason is that the compiler (atleast the version we are using) is somehow not able to optimize all the operations if CX is made a class.
One issue is - I dont want this to pass:
CX x; long p; p = x;
I assume the casting solution below will implicitly make the code for long/short etc pass too. (If not, then it is exactly what I am looking for!).
On a related note, answering David's question - the reason I want to refactor is - we want the ability to switch CX to be 32 bit or 64 bit. As such, we want to disallow any implicit conversions and catch them at compile time. Now, the reverse - (disallowiong
CX x = some_64_bit_int;
but allowing
CX x = some_32_bit_int;
I achieved by using a templatized = operator that asserts on compile time by default, but overloading it for my desired type.
In-case you feel this is a bad design or that I should try other alternatives - The reason why I need is this: We have thousands of lines of legacy code, where something is just typedefed to "int".
typedef int CX;
There are assignments all over the place like:
CX x; int k; k = x; // Writing the simplified relevant code here
I am working on a project to change CX to a class. In the first phase, we want to fix all compilation errors (that come on making CX a class) making as little changes to code as possible.
You can have CX as a class and have Conversion functions in the class for type int. That way your class can work this way
You could add a cast operator to your class if the only thing you want is conversion to int.
You cannot do this.
operator=()
has to be a member function and cannot be overloaded forint
. I see these possibilities:int get() const {return data;}
and call this.int
in a class, but still want to allow assignment to plainint
. That smells.