我试图弄清楚如何typeid
与Microsoft C ++ 2015一起使用。来自https://msdn.microsoft.com/zh-cn/library/fyf39xec.aspx的示例按原样工作,但是当我添加明显无害的额外功能时行时,编译器给出了一个错误。
// compile with: /GR /EHsc
#include <iostream>
#include <typeinfo.h>
class Base {
public:
virtual void vvfunc() {}
};
class Derived : public Base {};
using namespace std;
int main() {
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( *pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( *pd ).name() << endl; //prints "class Derived"
auto t = typeid(pb);
}
最后一行auto t = typeid(pb);
是我添加的,错误是
a.cpp(20): error C2248: 'type_info::type_info': cannot access private member declared in class 'type_info'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vcruntime_typeinfo.h(104): note: see declaration of 'type_info::type_info'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vcruntime_typeinfo.h(63): note: see declaration of 'type_info'
如果整个事情都失败了,我不会感到惊讶,但是如果最后一行没有失败,我看不到其余的工作方式。我想念什么?
嗯,这只是因为auto
尝试复制引用的对象,但是在这里无法完成。如果您改为说,它会起作用auto&
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句