C ++使用struct作为某种类型

时间:2018-05-21 23:15:35

标签: c++ struct

假设我有这个结构:

filtered = df.join(df_filter, on=["angle", "radius"], 
                   how='inner', lsuffix='_orig')
#    angle_orig  radius_orig  angle  radius
#0            0          500      0     500
#1            0         1000      0    1000
#2            0         1500      0    1500
#3           45          500      0    2000
#4           45         1000      0    2500
#5           45         1500      0    3000
#6           45         2000      0    4000
#7           45         2500      0    5000
#8           45         3000      0    6000
#9           90          500      0    7000
#10          90         1000     45     500
#11         135         2000     45    1000
#12         135         2500     45    1500

filtered.drop(["angle_orig", "radius_orig"], inplace=True)

是否可以直接将shape 用作int ?在这种情况下,形状将采用其类型的值。例如:

struct shape{
    int type;
    shape(){}
    shape(int _type){type = _type;}
};

一般情况下,是否可以使用结构,以便它假定其属性之一的选定值?在形状的情况下,它是一个int,但它可以是一个字符串或任何其他类型。

编辑:我尝试了@ Jarod42建议的代码,使用string作为类型:

shape s(2);
if     (s == 1) cout<<"this shape is a circle"<<endl;
else if(s == 2) cout<<"this shape is a triangle"<<endl;
else if(s == 3) cout<<"this shape is a rectangle"<<endl;
//...

当我写作

struct shape{
    string type;
    shape(){}
    shape(string _type){type = _type;}
    operator string() const {return type;}
}; 

它说错误:'operator =='不匹配(操作数类型是'shape'和'std :: string),虽然使用int作为类型,但它工作正常。

4 个答案:

答案 0 :(得分:5)

您可以添加operator int

struct shape{
    int type;
    shape(){}
    shape(int _type) : type(_type) {}

    operator int() const { return type; }
};

答案 1 :(得分:3)

通常,您可以使用operator T,其中T是您要投射到的类型。如果您想强制使用static_cast<T>(my_shape),那么您可以在前面添加关键字explicit

struct shape
{
    int type;

    shape() = default;
    shape(int _type) : type{ _type } {}

    operator int() const { return type; }
    explicit operator float() const { return type; }

    operator std::string() const
    {
        switch (type)
        {
            case 1:
                return "circle";
            case 2:
                return "triangle";
            case 3:
                return "rectangle";
            default:
                break;
        }
        return "(unknown shape)";
    }
};

这适用于内置类型(intfloatdouble),标准类型(std::stringstd::complex),自定义结构/类类型,甚至是指针或引用(例如,如果您有一个静态数组值)。您可能需要考虑是否需要这些转换运算符(封装是一个完整的其他讨论),但这在概念上是如何实现的。

在此示例中,您可能还想引入enum来存储您的类型值:

struct shape
{
    enum type_t
    {
        circle = 1,
        triangle,
        rectangle
    };

    type_t type;

    operator std::string() const
    {
        switch (type)
        {
            case circle:
                return "circle";
            case triangle:
                return "triangle";
            case rectangle:
                return "rectangle";
            default:
                break;
        }
        return "(unknown shape)";
    }

    shape(type_t _type) : type{ _type } {}

    // rest of class as before
};

答案 2 :(得分:2)

虽然使用强制转换运算符是一种方法,如@ Jarod42所示,我建议使操作更明确。

  1. 添加一个函数来获取类型。
  2. 使用函数调用和if-else语句中的返回值。
  3. struct shape{
        int type;
        shape(){}
        shape(int _type){type = _type;}
        int getType() const { return type;}
    };
    

    然后

    shape s(2);
    int type = s.getType();
    if     (type == 1) cout<<"this shape is a circle"<<endl;
    else if(type == 2) cout<<"this shape is a triangle"<<endl;
    else if(type == 3) cout<<"this shape is a rectangle"<<endl;
    

答案 3 :(得分:2)

如果您最终要做的就是按字符串比较形状,那么有一个更简单且更安全的解决方案:

struct shape{
    std::string type;

    shape() {}

    bool operator==(std::string const& rhs) const {
      return type == rhs;
    }
};

// and if you want to be able to do string == shape.
bool operator==(std::string const& lhs, shape const& rhs) {
  return rhs == lhs;
}
相关问题