简体   繁体   English

如何在C ++中的运行时确定数据类型?

[英]How can I determine data type during runtime in C++?

I have a class B which derives from A: 我有一个从A派生的B类:

template<class T>
class A
{
    class iterator; // Defined fully

    iterator begin ()
    {
        // Returns a pointer to the first element
    }
    iterator end ()
    {   
        // Returns a pointer to the last element
    }
}
template <class T>
class B : public A
{
    // It automatically inherits the iterator
}

template <typename InputIterator>
void foo (InputIterator first,InputIterator last)
{
    // Some code to infer whether it is of type A or B
}

Now some function say foo() is called using B::begin() at one time and sometime with A::begin() . 现在,有些函数说foo()是一次使用B::begin()调用的,而有时是通过A::begin()调用的。

I need to determine type during runtime to infer type and set some flag variables. 我需要在运行时确定类型以推断类型并设置一些标志变量。 How do I do this? 我该怎么做呢? I tried using typeinfo() but it returns the same value for both the iterators. 我尝试使用typeinfo()但它为两个迭代器返回相同的值。

From library type_traits you can use some type magic: 在库type_traits中,您可以使用一些类型魔术:
is_base_of - returns true if Base is base of Derived. is_base_of-如果Base是Derived的底数,则返回true。
is_same - returns true if A is the same type as B. is_same-如果A与B的类型相同,则返回true。
Everything with type_traits can be found here http://www.cplusplus.com/reference/type_traits/?kw=type_traits 带有type_traits的所有内容都可以在这里找到http://www.cplusplus.com/reference/type_traits/?kw=type_traits

They are not so runtime, it's only some magic with structs and templates, C++ does not support type as data by default. 它们不是运行时,而是结构和模板的魔力,C ++默认不支持将类型作为数据。 If you want so you can use Boost library, it does support types as I know. 如果愿意,可以使用Boost库,它确实支持我所知道的类型。

UPD: UPD:
As comments under the question mentioned A::iterator is absolutely the same with B::iterator, so without looking at classes they are the same memory chunk. 正如在该问题下提到的注释一样,A :: iterator与B :: iterator完全相同,因此,不查看类,它们是相同的内存块。
So solution (maybe) is to create a little different function, what depends on classes actually: 因此,解决方案(也许)是创建一个稍微不同的函数,实际上取决于类:

 template <typename LeftClass, typename RightClass>
 void foo (LeftClass left, RightClass right)
 { 
     if(is_same<LeftClass, RightClass>::value)
     {

     }
 //Or that
     if(is_same<LeftClass, A>::value && is_same<RightClass, A>::value)
 }

Just don't forget to make this "friend" with classes. 只是别忘了与班上的这个“朋友”。

typeinfo() returns the same value, because both A::begin() and B::begin() give you a value of the same type . typeinfo()返回相同的值,因为A::begin()B::begin()为您提供了相同类型的值。

You should either have B::iterator inherit from A::iterator or have a special function in your iterator that returns a reference/pointer to it's container (which is then either of type A or type B ). 您应该让B::iteratorA::iterator B::iterator继承,或者在A::iterator有一个特殊的函数,该函数将对其容器的引用/指针返回(然后是类型A或类型B )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时确定C ++的类型 - runtime determine type for C++ 在C ++中运行时调用模板时,是否可以切换模板的参数类型? - Can I switch an template's parameter type during calling it at runtime in C++? 如何在C ++中确定运行时的实际对象类型; - How to determine actual object type at runtime in C++; C ++运行时如何确定抛出异常的类型? - How does the C++ runtime determine the type of a thrown exception? 如何确定运行时void *指针是指向Objective-C对象还是C ++对象 - How can I determine whether a void * pointer points to a objective-c object or a c++ object at runtime C ++在运行时确定多态对象的类型 - C++ Determine the type of a polymorphic object at runtime 如果没有 RTTI,在 C++ 中,我如何在运行时确定集合中的 object 是否实现接口 - Without RTTI, in C++ how can I determine at runtime if an object in a collection implements an interface 我如何以编程方式确定我的C ++运行时库的位置? - How can I programatically determine where my C++ runtime libraries are? 如何在运行时确定是否存在特定C ++异常类的catch块? - How can I determine at runtime whether there is a catch block for a particular C++ exception class? 如何在.NET下的C / C ++中确定运行时的方法参数类型? - How to determine method parameter types during runtime in C/C++ under .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM