C++ - 重载默认类型的赋值运算符
我想重载“int”、“long”等类型的赋值运算符。也就是说,我想使用如下代码:
class CX {
private:
int data;
...
};
CX obj;
int k;
k = obj; // k should get the value of obj.data
显然赋值运算符不能是友元函数。我如何实现上述目标?
我可能错过了一些简单的东西,但只是无法弄清楚执行此操作的语法/方法。
此外,还有一个 IMP 限制 - 我们不能使用 get/set 方法,因为:: 在发布代码中,我们将 CX 类型定义为 int 或 long(根据需要),但在 DEBUG 代码中,我们希望将其作为一个类(用于在数千个位置进行自动类型检查)。代码必须是通用的。 原因是,如果 CX 成为一个类,编译器(至少我们正在使用的版本)在某种程度上无法优化所有操作。
一个问题是 - 我不希望这一点通过:
CX x; long p; p = x;
我假设下面的转换解决方案也会隐式地使长/短等代码通过。 (如果没有,那么这正是我正在寻找的!)。
相关说明,回答 David 的问题 - 我想要重构的原因是 - 我们希望能够将 CX 切换为 32 位或 64 位。因此,我们希望禁止任何隐式转换并在编译时捕获它们。现在,相反 - (不允许
CX x = some_64_bit_int;
但允许
CX x = some_32_bit_int;
我通过使用默认在编译时断言的模板化 = 运算符来实现,但将其重载为我想要的类型。
以防万一觉得这是一个糟糕的设计,或者我应该尝试其他替代方案 - 我需要的原因是这样的:我们有数千行遗留代码,其中某些内容只是类型定义为“int”。
typedef int CX;
有到处都是这样的作业:
CX x; int k; k = x; // Writing the simplified relevant code here
我正在开发一个将 CX 更改为类的项目。在第一阶段,我们希望修复所有编译错误(使 CX 成为一个类)。尽可能少地更改代码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您唯一想要的就是转换为 int,您可以向您的类添加强制转换运算符。
You could add a cast operator to your class if the only thing you want is conversion to int.
你不能这样做。
operator=()
必须是成员函数,不能为int
重载。我看到了这些可能性:int get() const {return data;}
并调用它。int
包装在类中,但仍希望允许对普通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.您可以将 CX 作为一个类,并在该类中为 int 类型提供转换函数。
这样你的班级就可以这样运作
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