我对 C++ 相当陌生,我主要使用 python。我正在尝试检查存储在我正在处理的对象中的值的变量类型。我记得Python中有一个命令isinstance
我可以使用它作为运行某些命令的条件,例如如果下一个值是字符串,则执行 A,如果是 int,则执行 B。
有没有办法快速检查 C++ 中变量的数据类型?
Example:
在 python 中,我有一个带有数学运算的数组,每个字符都在一个字段中
[3,"+",2]
当我读取数组时,我将使用 isinstance 命令将整数与字符串分开
if isinstance(list[0],int):
aux1.append(list[0])
list=list[1:]
else:
if isinstance(lista[0],str):
aux2.append(list[0
list=list[1:]
现在在 C++ 中我需要做类似的事情,但是这次每个字符都在链表的节点中,并且我需要将它们分开,链表中的整数和另一个链表中的字符串
您似乎正在努力解决的是 C++ 是一种静态且(相对)强类型的语言。有关这些术语的实际含义的讨论,我参考这另一个问题 https://stackoverflow.com/q/2351190/1116364,正如它所解释的那样,可能比我能做得更好。
首先,您应该确保您确实需要按照当前尝试的方式做事。不要尝试编写 Python 风格的代码。
也就是说,基本上有两种不同的方法可以实现与 Python 类似的行为(动态类型、鸭子类型,因此相对较弱的类型):
-
使用 C++ 内置的动态类型机制。因此,您需要创建一个所谓的多态基类,这是一个至少有一个虚拟成员函数的类(如果你没有定义的接口,析构函数也可以工作 - 它通常也可以must是虚拟的以避免出现令人讨厌的问题)。一个简短的例子:
struct Value {
virtual void write_to(std::ostream &) const = 0;
virtual void read_from(std::istream &) = 0;
virtual ~Value() {} // Absolutely required!!!
};
struct Number : public Value {
int data;
void write_to(std::ostream & stream) const {
stream << "<Number " << data << ">";
}
void read_from(std::istream & stream) {
stream >> data; // Not the same format as write_to, shame on me
}
// Implicit destructor is fine
};
struct String : public Value {
std::string data;
void write_to(std::ostream & stream) const {
stream << "<String " << data.size() << " " << data << ">";
}
void read_from(std::istream & stream) {
stream >> data; // Not the same format as write_to, shame on me
}
};
使用这个你现在可以存储Value
您可以让用户决定其实际类型:
std::vector<std::unique_ptr<Value>> values;
while (wantsToEnterMoreValues) {
std::string choice = ask("What type of value do you want to enter?");
std::unique_ptr<Value> value;
if (choice == "string") {
value = std::make_unique<String>();
} else if (choice == "number") {
value = std::make_unique<Number>();
} else {
// launch apocalypse
}
value->read_from(std::cin);
values.push_back(value);
}
这很容易扩展到更多类型。请注意,为了使用 C++ 的内置动态类型,您需要不使用值语义,而是完全使用引用语义,或者使用真实引用,或者(在大多数情况下,所有权必须转移,就像上面的示例中的values
矢量)使用指针。
The dynamic_cast
方法的工作原理与此非常相似,除了您更明确地使用运行时类型信息并且不需要统一的接口(但需要更多的工作来维护代码)。
-
Use the union
语言特征。这只有在 C++11 中才真正成为可能,其中联合成员可能是非平凡可解释的:
enum class Type {
Number, String
};
struct Value {
Type type;
union {
std::string string;
int number;
};
Value(std::string const & s) : type(Type::String), string(s) {}
Value(int n) : type(Type::Number), number(n) {}
Value(Value const & v) : type(v.type) {
switch (type) {
case Type::Number: number = v.number; break;
case Type::String: new (&string) std::string(v.string); break;
default: break; // Launch nuclear missiles
}
}
~Value() {
switch (type) {
case Type::String: string.~std::string(); break;
default: break;
}
}
};
如您所见,这需要大量工作。通过这种方法,您可以使用值语义,但不能轻松扩展您的Value
支持更多类型。此外,由于使用union
,你会浪费一些内存。
底线:您需要自己实现该行为,但可以完全按照您希望的行为方式来实现。例如:您还可以实现赋值运算符operator=(Value const &)
进行隐式类型转换。您还可以使用来自的实现boost, like boost::any
or boost::variant
.
我想引用我在这个网站上写的关于同一主题的两个答案,也许它们对您也有帮助:
- https://stackoverflow.com/a/32926607/1116364 https://stackoverflow.com/a/32926607/1116364
- https://stackoverflow.com/a/32041381/1116364 https://stackoverflow.com/a/32041381/1116364
还有一些相关的 C 代码,因为它试图解决同样的问题:https://stackoverflow.com/a/35443434/1116364 https://stackoverflow.com/a/35443434/1116364
注意:此答案中的所有代码都是直接从内存中编写的,未经测试。因此,它仅用作基本技术的演示。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)