C ++-重载默认类型的赋值运算符 [英] C++ - overloading assignment operator for default types

查看:72
本文介绍了C ++-重载默认类型的赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重载类型为"int","long"等的赋值运算符.也就是说,我想使用如下代码:

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.

此外,一个IMP限制-我们不能使用get/set方法,因为:: 在发布代码中,我们将CX typedef定义为int或long(根据需要),但是在DEBUG代码中,我们希望将其作为类(用于在数千个地方进行自动类型检查).该代码必须是通用的. 原因是,如果将CX设置为类,则编译器(至少是我们使用的版本)无法优化所有操作.

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!).

在相关说明中,回答David的问题-我要重构的原因是-我们希望能够将CX切换为32位或64位.因此,我们要禁止任何隐式转换并在编译时捕获它们.现在,相反-(disallowiong

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;

CX x = some_64_bit_int;

但允许

CX x = some_32_bit_int;

CX x = some_32_bit_int;

我使用了默认的编译时断言的templatized =运算符来实现,但是将其重载为所需的类型.

I achieved by using a templatized = operator that asserts on compile time by default, but overloading it for my desired type.

以防万一,您认为这是一个糟糕的设计,或者我应该尝试其他替代方法-我需要的原因是:我们有数千行遗留代码,其中某些类型只是类型定义为"int"

typedef int CX;

到处都有作业,例如:

CX x; int k; k = x;    // Writing the simplified relevant code here

我正在研究一个将CX更改为类的项目.在第一阶段,我们要修复所有编译错误(使CX成为类),这些错误将对代码的更改尽可能少.

推荐答案

您不能执行此操作. operator=()必须是成员函数,并且不能为int重载.我看到了这些可能性:

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

  • 依赖类中的隐式转换运算符.我对此表示反对.我不记得有一次我对此感到后悔,后来又将其删除了.
  • 编写一个显式成员函数int get() const {return data;}并调用它.
  • 问问自己为什么要将int包装在类中,但仍然希望允许分配给普通int.闻起来
  • 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.

这篇关于C ++-重载默认类型的赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆