示例代码:
const char* a = "CD" ;
a[0] = 'S' ; // should not compile
然后我尝试检查类的vtable,输出为:
class base {
int val1;
public:
base(int val): val1(val)
{
}
virtual void basefunc()
{
std::cout << "I am in base func";
}
virtual ~base()
{
std::cout << "I am in base destructor";
}
};
class derived: public base
{
int val2;
public:
derived(int val): base(100), val2(val)
{
}
virtual void basefunc() {
std::cout << "I am in derived func";
}
virtual ~derived()
{
std::cout << "I am in derived destructor";
}
};
int main()
{
base b = base(10);
derived d = derived(2);
base *ptr = &b;
ptr->basefunc();
ptr = &d;
ptr->basefunc();
}
输出特定于使用gdb符号编译的clang。
我无法理解第4/5个索引处的数据。这两个索引所指向的信息类型以及如何使用。 我了解每个编译器都有选择以自己想要的方式实现。
PS:这是一个后续问题 Analyzing vtable in c++”