重载c++实类型和自定义复杂类型之间的强制转换操作符()

Overloading casting operator() between C++ real types and customized complex types

本文关键字:类型 转换 操作符 之间 c++ 自定义 复杂 重载      更新时间:2023-10-16

我为我的应用程序定义了三个类: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, doubleint2_, float2_double2_之间所有可能的赋值。

我现在想重载强制转换()操作符。例如,为了重载从intfloat2_的强制转换,我在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 */ }