如何在 C++ 中键入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to typeof in C++
提问by Velho Kerho
How to simulate C# typeof-command behavior in C++?
如何在 C++ 中模拟 C# typeof-command 行为?
C# example:
C# 示例:
public static PluginNodeList GetPlugins (Type type)
{
...
}
Call:
称呼:
PluginManager.GetPlugins (typeof(IPlugin))
How to implement this using C++? Maybe QT or Boost libraries provide a solution?
如何使用 C++ 实现这一点?也许 QT 或 Boost 库提供了解决方案?
What about the case if you want to implement .GetPlugins(...) in a way that it loads those kinds of objects from a file (.so or .dll)?
如果您想以从文件(.so 或 .dll)加载这些类型的对象的方式实现 .GetPlugins(...),情况如何?
回答by daveg
You could use a dynamic_cast to test types as shown below:
您可以使用 dynamic_cast 来测试类型,如下所示:
IPlugin* iPluginPtr = NULL;
iPluginPtr = dynamic_cast<IPlugin*>(somePluginPtr);
if (iPluginPtr) {
// Cast succeeded
} else {
// Cast failed
}
回答by Charles
This behaviour is called RTTI (Run time type information). This technique is best to be avoided, but can be beneficial in some situations.
这种行为称为RTTI(运行时类型信息)。最好避免这种技术,但在某些情况下可能是有益的。
There are two big ways to solve this. The first way is to write an interface with a pure virtual function that returns a class specific integer reference code. This code can then be used to represent a specific type. These integers could be stored in a specific enumeration.
有两种大方法可以解决这个问题。第一种方法是编写一个带有纯虚函数的接口,该函数返回一个特定于类的整数引用代码。然后可以使用此代码来表示特定类型。这些整数可以存储在特定的枚举中。
In derived classes you can then override the method and return that class specific type. During runtime, you can then call Plugin->getType() for instance, and it'll return its specific type. You can then perform a static_cast on the pointer to get the correct pointer of the derived type back.
在派生类中,您可以重写该方法并返回该类的特定类型。例如,在运行时,您可以调用 Plugin->getType(),它会返回其特定类型。然后,您可以对指针执行 static_cast 以获取派生类型的正确指针。
The second way is to either use typeid to get the classtype of the object; but this is compiler dependant. You can also try casting your pointer using dynamic_cast; dynamic_cast returns a null pointer when it's being cast into the wrong type; and a valid one when being cast in a correct type. The dynamic cast method has a bigger overhead tho than the getType method described above.
第二种方式是要么使用typeid获取对象的classtype;但这取决于编译器。你也可以尝试使用 dynamic_cast 来投射你的指针;dynamic_cast 在被转换为错误类型时返回一个空指针;并且在以正确的类型进行转换时是有效的。动态转换方法比上面描述的 getType 方法有更大的开销。
回答by csl
If you want complete typeof-like behaviour, you would have to use RTTI (run-time type information). On many compilers you have to explicitly activate usage of RTTI, as it incurs run-time overhead.
如果您想要完整的类似 typeof 的行为,则必须使用RTTI (run-time type information)。在许多编译器上,您必须显式激活 RTTI 的使用,因为它会导致运行时开销。
Then you can use typeidor dynamic_castto find an object's type.
然后您可以使用typeid或dynamic_cast来查找对象的类型。
If you don't want to use typeid, you'd have to use inheritance, pointers and/or overloads. Boost mighthelp you, but it's not too hard.
如果您不想使用 typeid,则必须使用继承、指针和/或重载。Boost可能会帮助你,但它并不太难。
Example 1:
示例 1:
class Iplugin { ... }
class Plugin1 : public Iplugin { ... }
class Plugin2 : public Iplugin { ... }
void getplugins(Iplugin* p) {
// ... you don't know the type, but you know
// what operations it supports via Iplugin
}
void getplugins(Plugin1& p) {
// expliticly handle Plugin1 objects
}
As you can see there are several ways of avoiding usage of RTTI and typeid.
如您所见,有多种方法可以避免使用 RTTI 和 typeid。
回答by Adisak
You can use typeof() in GCC. With other compilers, it's either not supported or you have to do crazy template mangling or use "bug-features" that are very compiler specific (like the way Boost does it).
您可以在 GCC 中使用 typeof()。对于其他编译器,它要么不受支持,要么您必须进行疯狂的模板修改或使用非常特定于编译器的“错误功能”(就像 Boost 那样)。
回答by Adisak
Designing around this problem would be the best choice. Good use of object orientation can usually help but you can always create your own system for querying the type of an object by using a base class which stores an identifier for each object, for instance.
围绕这个问题进行设计将是最好的选择。良好地使用面向对象通常会有所帮助,但您始终可以创建自己的系统来查询对象的类型,例如,通过使用存储每个对象的标识符的基类。
Always try to avoid using dynamic_cast as it most often uses string comparison to find the type of an object and that makes it really slow.
总是尽量避免使用 dynamic_cast,因为它最常使用字符串比较来查找对象的类型,这使得它非常慢。
回答by Jerry Coffin
Boost does have a typeof. C++ 0x doesn't call it typeof, but has both 'auto' and 'decltype' that provide the same kinds of functionality.
Boost 确实有一个 typeof。C++ 0x 不称其为 typeof,但具有提供相同类型功能的“auto”和“decltype”。
That said, I'm pretty sure none of those provides what you're really looking for in this case -- at most, they provide only a small piece of what you need/want overall.
也就是说,我很确定这些都不能提供您在这种情况下真正需要的东西——最多,它们只提供了您整体需要/想要的一小部分。
回答by user2023370
Surely you would just use overloading?
你肯定会使用重载吗?
static PluginManager::GetPlugins(Type1 &x) {
// Do something
}
static PluginManager::GetPlugins(Type2 &x) {
// Do something else
}
and then call:
然后调用:
PluginManager::GetPlugins(IPlugin);
回答by éric Malenfant
Not directly answering the "how to get typeof() in C++", but I infer from your question that you are looking at how to do plugins in C++. If that's the case, you may be interested in the (not-yet)Boost.Extensionlibrary, and maybe in its reflectionpart.
没有直接回答“如何在 C++ 中获取 typeof()”,但我从你的问题中推断你正在研究如何在 C++ 中做插件。如果是这种情况,您可能对(还没有)Boost.Extension库感兴趣,并且可能对它的反射部分感兴趣。