C++将结构用作某种类型
C++ use struct as some type
>假设我有这个结构:
struct shape{
int type;
shape(){}
shape(int _type){type = _type;}
};
是否可以直接将形状用作整数?在这种情况下,形状将采用其类型的值。例如:
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;
//...
通常,是否可以使用结构,以便它假定其属性之一的选定值?在 shape 的情况下,它是一个 int,但它可以是字符串或任何其他类型。
编辑:我尝试了@Jarod42使用字符串作为类型建议的代码:
struct shape{
string type;
shape(){}
shape(string _type){type = _type;}
operator string() const {return type;}
};
当我写的时候
shape s1("circle");
string s2 = "circle";
if(s1 == s2){ ...
它说错误:与"运算符=="不匹配(操作数类型是"形状"和"std::string">(,尽管使用 int 作为类型,它工作正常。
您可以添加operator int
:
struct shape{
int type;
shape(){}
shape(int _type) : type(_type) {}
operator int() const { return type; }
};
通常,您可以使用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)";
}
};
这将适用于内置类型(int
,float
,double
(,标准类型(std::string
,std::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
};
虽然使用 cast 运算符是一种方法,如 @Jarod42 所示,但我建议使操作更明确一些。
- 添加一个函数来获取类型。
- 在
if-else
语句中使用函数调用和返回值。
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;
如果您最终只想按字符串比较形状,那么有一个更简单、更安全的解决方案:
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;
}
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 任何种类的分数 在任何类型的订单中
- 2 种模板相关类型种类的差异
- C++每帧更新和复制一系列值.我应该使用哪种类类型
- 在键上使用 map.find() 和 count(),这是一种类对象类型