C++ - overloading assignment operator for default

2019-08-21 01:01发布

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.

3条回答
Ridiculous、
2楼-- · 2019-08-21 01:35

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

查看更多
相关推荐>>
3楼-- · 2019-08-21 01:38

You could add a cast operator to your class if the only thing you want is conversion to int.

class ABC
{
public:
    operator int() { return data; } // cast operator
private:
    int data;
...
};
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-08-21 01:43

You cannot do this. operator=() has to be a member function and cannot be overloaded for int. I see these possibilities:

  • Rely on an implicit conversion operator in your class. I would advice against that. I cannot remember a single time I have done this where I have not regretted and removed it later.
  • Write an explicit member function int get() const {return data;} and call this.
  • Ask yourself why you want to wrap that int in a class, but still want to allow assignment to plain int. That smells.
查看更多
登录 后发表回答