a**U 发帖数: 115 | 1 来源于面试题目,下面的code可以编译运行成功。关键是我觉得不应该编译运行成功,
请看下面我的comments。请高手指点。
#include
using namespace std;
class A
{
public:
virtual void test(){ cout <<"test A"<
};
class B : public A
{
public:
virtual void test(){ cout <<"test B"<
};
class C : public A
{
int value;
public:
C(){value = 1; }
virtual void test(){ cout <<"test C"<
void accessValue() { cout << "value=" << value << endl; }
void setValue(int value){ this->value = value; }
};
int main()
{
B b;
C* c = (C*)&b;
A* a = &b;
c->A::test();
c->test();
c->setValue(100); //我觉得这里因该有问题,本身的对象是b,b应该没有
value这个member,我觉得应该是segment fault. 为何可以运行成功那?
c->accessValue();
A obja;
C* cc = static_cast(&obja);
cc->setValue(100);
cc->accessValue();
return 0;
} | b*****e 发帖数: 22 | 2 Probably due to the compiler you used. Empty base class optimization and
memory alignment all depend on compilers. Try print sizeof of all three
classes. Your compiler probably gives 4 bytes for all. I used GCC and got 4,
4,8 and therefore segfault at setValue. | a**U 发帖数: 115 | 3 我也打印出来,sizeof(A)=4, sizeof(B)=4, sizeof(C) = 8, 我用的ubuntun, 编译是
g++ cast.cpp 出来 a.out. 运行a.out,没有问题。奇怪的很! | t****t 发帖数: 6806 | 4 请跟我念十遍: undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
【在 a**U 的大作中提到】 : 我也打印出来,sizeof(A)=4, sizeof(B)=4, sizeof(C) = 8, 我用的ubuntun, 编译是 : g++ cast.cpp 出来 a.out. 运行a.out,没有问题。奇怪的很!
| a**U 发帖数: 115 | 5 多谢大牛!
1) undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
2) undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
3)undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
4)undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
5)
undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
6) undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
7) undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
8) undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
9) undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
10)undefined behaviour may lead to any result, including running
successfully, segfault, or formating your hard drive.
十遍完成!!!
running
【在 t****t 的大作中提到】 : 请跟我念十遍: undefined behaviour may lead to any result, including running : successfully, segfault, or formating your hard drive.
| d****n 发帖数: 1637 | 6 c pointer 用的是 object b 的地址,
但是它完全拥有 Class C 的 成员, 根据定义。
当然可以在内存相应位置找到int value. 虽然可能是越界的。
【在 a**U 的大作中提到】 : 来源于面试题目,下面的code可以编译运行成功。关键是我觉得不应该编译运行成功, : 请看下面我的comments。请高手指点。 : #include : using namespace std; : class A : { : public: : virtual void test(){ cout <<"test A"<: }; : class B : public A
|
|