重载c++实类型和自定义复杂类型之间的强制转换操作符()
Overloading casting operator() between C++ real types and customized complex types
我为我的应用程序定义了三个类:int2_
(整数对),float2_
(浮点对)和double2_
(双精度对),主要用于执行复杂的算术运算。
下面的讨论之后:
Discussion1
和
Discussion2
我实现了以下解决方案
class float2_;
class double2_;
class int2_ {
public:
int x;
int y;
__host__ __device__ int2_() : x(), y() {}
__host__ __device__ inline const int2_& operator=(const int a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const float a) { x = (int)a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const double a) { x = (int)a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const int2_ a) { x = a.x; y = a.y; return *this; }
__host__ __device__ inline const int2_& operator=(const float2_ a);
__host__ __device__ inline const int2_& operator=(const double2_ a);
};
class float2_ {
public:
float x;
float y;
__host__ __device__ float2_() : x(), y() {}
__host__ __device__ inline const float2_& operator=(const int a) { x = (float)a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const float a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const double a) { x = (float)a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const int2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
__host__ __device__ inline const float2_& operator=(const float2_ a) { x = a.x; y = a.y; return *this; }
__host__ __device__ inline const float2_& operator=(const double2_ a);
};
class double2_ {
public:
double x;
double y;
__host__ __device__ double2_() : x(), y() {}
__host__ __device__ inline const double2_& operator=(const int a) { x = (double)a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const float a) { x = (double)a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const double a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const int2_ a) { x = (double)a.x; y = (double)a.y; return *this; }
__host__ __device__ inline const double2_& operator=(const float2_ a) { x = (double)a.x; y = (double)a.y; return *this; }
__host__ __device__ inline const double2_& operator=(const double2_ a) { x = a.x; y = a.y; return *this; }
};
__host__ __device__ inline const int2_& int2_::operator=(const float2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
__host__ __device__ inline const int2_& int2_::operator=(const double2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
__host__ __device__ inline const float2_& float2_::operator=(const double2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
正确定义了int
, float
, double
和int2_
, float2_
和double2_
之间所有可能的赋值。
我现在想重载强制转换()
操作符。例如,为了重载从int
到float2_
的强制转换,我在float2_
类
__host__ __device__ inline const float2_& operator()(const int in) { x = (float)in; y=0.; return *this; };
不幸的是,它似乎没有效果。如果我尝试
float2_ a;
int b = 1;
a = (float2_)b;
编译器说
no suitable constructor exists to convert from "int" to "float2_"
我应该实现一个包装器类,比如int_
,来允许这种类型的转换吗?谢谢你。
不是重载强制转换操作符,而是重载()
操作符。也就是说,你定义了一个操作符,它应该这样使用:
float2_ a;
float2_ b = a(3); // This is the operator you are overloading
现在,您不能从内置类型(如int
)定义强制转换操作符,但您可以定义显式构造函数,它将或多或少地提供相同的功能:
class float2_
{
public:
//...
float2_(int in) { x = (float)in; y=0. };
}
使用它:float2_ f;
f = float2_(3); // Or directly float2_ f (3);
您已经重载了operator()
,接受int
并返回float2_&
。你可以这样使用:
float2_ a;
a(5); // Returns a float2_&
听起来像是需要一个转换操作符。它们的形式为operator type()
(注意没有返回类型)。因此,如果要将float2_
转换为int
,则需要在类定义
operator int() { /* Convert to int and return here */ }
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 逐位操作的隐式类型转换
- 模板中的类型转换
- 在 C++(和 C)中进行类型转换时明显不一致
- 字符类型转换不兼容
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- C++:用户定义的显式类型转换函数错误
- 将类指针类型转换为键时出错
- 通过引用传递参数时C++类型转换
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- C++显式类型转换(C 样式强制转换)的强制表示法和static_cast的多种解释
- C++无效的函数类型转换
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将Windows产品类型转换为名称?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 事件系统:使用类型转换或联合进行继承
- 如何在参数中定义隐式类型转换的构造函数?
- 类模板实例化中的类型转换