接受并识别任何类型的输入C ++ [英] Accept and identify any type of input C++
问题描述
我想写一个简单的C ++程序来创建一个链表。我想让这个列表能够在其容器中存储任何类型的数据。然而,我意识到我的主要问题是能够接受任何类型的输入和存储它。例如,如果变量 std :: cin
存储到字符串类型,则只接受字符串,并且必须在程序编译之前定义该变量。
I'm trying to write a simple C++ program that creates a linked list. I would like to make this list be able to store any type of data in its container. However, I realised that my main problem is being able to accept any type of input and storing it. For example, if the variable std::cin
stores to is of type string, only strings will be accepted, and this variable must be defined before program compilation.
我的问题是:是否有任何方式接受任何类型的输入与 std :: cin
(或任何其他输入法)然后根据输入的类型调用某些函数?
My question is: is there any way to accept any type of input with a std::cin
(or any other input method) and then call certain functions depending on the input's type?
...的逻辑...
Something along the logic of...
cin >> data
if (data.type == string)
{
cout << "data's type: string"
}
if (data.type == int)
{
cout << "data's type: int"
}
谢谢!
推荐答案
C ++(主要)静态类型。也就是说,变量的类型必须在编译时是已知的,并且不能在运行时改变。取决于一些用户输入。
C++ is (mostly) statically typed. That is, the types of variables have to be known at compile time and cannot change at runtime, e.g. depending on some user input.
这个规则的一个例外是多态类:当你有一个 virtual
成员函数,那么将有一种方法(很可能是一个指针作为该类的所有实例的成员)来区分该类的子类(和自身):
The one exception to this rule are polymorphic classes: When you have a base class with some virtual
member function, then there will be a way (most likely a pointer as a member of all instances of that class) to distinguish between sub classes of that class (and itself):
struct Base {
virtual ~Base() {}
};
struct SubA : public Base {};
struct SubB : public Base {};
// ...
Base const & instance = SubA{};
try {
SubA const & as_subA = dynamic_cast<SubA const &>(instance);
// The instance is a SubA, so the code following here will be run.
} catch (std::bad_cast const &) { /* handle that somehow */ }
使用此机制(或最好是虚拟函数本身),您可以根据实例的动态类型具有不同的行为,该实例只在运行时已知。
Using this mechanism, or preferably the virtual function itself, you can have different behavior depending on the dynamic type of an instance, which is only known at run time.
C ++是一种灵活的语言,当然也可以自己实现类似的东西:
C++ being a flexible language, you can - of course - also implement something similar on your own:
struct Thing {
enum class Type {
Integer, String, Vector
} type;
union {
int integer;
std::string string;
std::vector<int> vector;
} data;
// Plus a lot of work in constructors, destructor and assignment, see rule of 5
};
使用类似这样的方法可以让对象具有动态的类型根据对象在运行时实际具有的类型来做不同的事情。
Using something like this allows you to have objects with a "type" of dynamic nature, and are able to do different things depending on what type the object actually has at run time.
但是当然你不需要自己写(虽然它不那么难),有很多实现像例如 boost :: any
和 boost :: variant
。
But of course you don't need to write that on your own (though its not that hard), there are a lot of implementations like for example boost::any
and boost::variant
.
这篇关于接受并识别任何类型的输入C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!