#include <iostream>
struct Base1 {
int val1;
};
struct Base2 {
int val2;
};
struct Derived : Base1, Base2 {
//...
};
int main()
{
std::cout << (int)(&((Base1*)0)->val1) << std::endl;
std::cout << (int)(&((Base2*)0)->val2) << std::endl;
std::cout << (int)(&((Derived*)0)->val1) << std::endl;
std::cout << (int)(&((Derived*)0)->val2) << std::endl;
printf("&Base1::val1 = %p \n", &Base1::val1);
printf("&Base2::val2 = %p \n", &Base2::val2);
printf("&Derived::val1 = %p \n", &Derived::val1);
printf("&Derived::val2 = %p \n", &Derived::val2);
return 0;
}

std::cout << (int)(&((Derived*)0)->val2) << std::endl;
和
printf("&Derived::val2 = %p \n", &Derived::val2);
不都是求偏移嗎,還是我理解錯了?
uj5u.com熱心網友回復:
int Base1::* p1 = &Derived::val1;
int Base2::* p2 = &Derived::val2;
Derived d;
d.*p1 = 1;
d.*p2 = 2;
debug一下,p1與p2是啥值?
uj5u.com熱心網友回復:
優先級別的問題cout<<&(((Base1*)0)->val);//結構體指向0地址(0地址相當于null,估計要報錯)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/227419.html
標籤:C++ 語言
下一篇:新手求教
