在下面程序的最后两行中,
可能的输出: https://ideone.com/WoX5DI
static_cast<void*>
和dynamic_cast<void *>
的行为不同。据我所知,dynamic_cast<void*>
的结果始终解析为完整对象的地址。因此它以某种方式使用了RTTI。请问有人能解释编译器如何使用RTTI来区分这两者吗?#include <iostream>
using namespace std;
class Top {
protected:
int x;
public:
Top(int n) { x = n; }
virtual ~Top() {}
friend ostream& operator<<(ostream& os, const Top& t) {
return os << t.x;
}
};
class Left : virtual public Top {
protected:
int y;
public:
Left(int m, int n) : Top(m) { y = n; }
};
class Right : virtual public Top {
protected:
int z;
public:
Right(int m, int n) : Top(m) { z = n; }
};
class Bottom : public Left, public Right {
int w;
public:
Bottom(int i, int j, int k, int m): Top(i), Left(0, j), Right(0, k) { w = m; }
friend ostream& operator<<(ostream& os, const Bottom& b) {
return os << b.x << ',' << b.y << ',' << b.z<< ',' << b.w;
}
};
int main() {
Bottom b(1, 2, 3, 4);
cout << sizeof b << endl;
cout << b << endl;
cout << static_cast<void*>(&b) << endl;
Top* p = static_cast<Top*>(&b);
cout << *p << endl;
cout << p << endl;
cout << static_cast<void*>(p) << endl;
cout << dynamic_cast<void*>(p) << endl;
return 0;
}
可能的输出: https://ideone.com/WoX5DI
28
1,2,3,4
0xbfcce604
1
0xbfcce618
0xbfcce618
0xbfcce604
void*
动态转换的方式与将其转换成派生类指针的方式相同。在典型的实现中,它会查找指针所指向的对象的虚函数表(vptr),并使用该表来确定最终派生对象的类型。然后可以相应地计算偏移量。 - Brian Bidynamic_cast<void*>
- 这有意义吗?任何使用它的代码都应该无法通过合理的审核。 - BЈовићdynamic_cast
到void *
是dynamic_cast
的一个专门的、特定的功能,是有意设计成这样的。我在实践中从未使用过它,但显然有人认为它具有实际意义。 - AnT stands with Russia