0

我为我的应用程序定义了三个类:(int2_整数对)、float2_(浮点数对)和double2_(双精度数),本质上是为了执行复杂的算术运算。

经过以下讨论:

讨论1

讨论2

我实施了以下解决方案

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_

我现在想重载强制转换()运算符。为了重载,例如从intto的转换,我在类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_允许这种类型的转换吗?谢谢你。

4

2 回答 2

1

您已经超载operator()了获取 aint并返回 a float2_&。你会像这样使用它:

float2_ a;
a(5); // Returns a float2_&

相反,听起来您想要一个转换运算符。他们有形式operator type()(注意没有返回类型)。因此,如果要从 a 转换为 a float2_,则int需要在类定义中使用以下内容

operator int() { /* Convert to int and return here */ }
于 2013-04-26T08:10:04.707 回答
1

您不是重载强制转换运算符,而是重载运算()符。也就是说,您正在定义一个应该以这种方式使用的运算符:

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);
于 2013-04-26T08:10:54.457 回答