A Za, A Za, Fighting...

坚信:勤能补拙

C++基础题

题目来源:
http://zhedahht.blog.163.com/blog/static/25411174201102642136998/

题目(六):运行下列C++代码,输出什么?

struct Point3D

{

        int x;

        int y;

        int z;

};

 

int _tmain(int argc, _TCHAR* argv[])

{

        Point3D* pPoint = NULL;

        int offset = (int)(&(pPoint)->z);

 

        printf("%d", offset);

        return 0;

}

答案:输出8。由于在pPoint->z的前面加上了取地址符号,运行到此时的时候,会在pPoint的指针地址上加z在类型Point3D中的偏移量8。由于pPoint的地址是0,因此最终offset的值是8

&(pPoint->z)的语意是求pPoint中变量z的地址(pPoint的地址0z的偏移量8),并不需要访问pPoint指向的内存。只要不访问非法的内存,程序就不会出错。

题目(七):运行下列C++代码,输出什么?

class A

{

public:

        A()

        {

                Print();

        }

        virtual void Print()

        {

                printf("A is constructed.\n");

        }

};

 

class B: public A

{

public:

        B()

        {

                Print();

        }

 

        virtual void Print()

        {

                printf("B is constructed.\n");

        }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

        A* pA = new B();

        delete pA;

 

        return 0;

}

答案:先后打印出两行:A is constructed. B is constructed. 调用B的构造函数时,先会调用B的基类及A的构造函数。然后在A的构造函数里调用Print。由于此时实例的类型B的部分还没有构造好,本质上它只是A的一个实例,他的虚函数表指针指向的是类型A的虚函数表。因此此时调用的PrintA::Print,而不是B::Print。接着调用类型B的构造函数,并调用Print。此时已经开始构造B,因此此时调用的PrintB::Print

同样是调用虚拟函数Print,我们发现在类型A的构造函数中,调用的是A::Print,在B的构造函数中,调用的是B::Print。因此虚函数在构造函数中,已经失去了虚函数的动态绑定特性。



posted on 2011-06-22 19:41 simplyzhao 阅读(228) 评论(0)  编辑 收藏 引用 所属分类: M_面试题集锦


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜