你以为你真的精通 C++ 吗 -- 回答下面的问题再说吧!

bgsn 2001-03-26 05:17:00
#include <stdio.h>

public class X
{
public:
X() {
foo();
bar();
}

void bar() {
foo();
}

virtual void foo() {
printf("X::foo\n");
}
};

public class Y: public X
{
public:
virtual void foo() {
printf("Y::foo\n");
}
};

void main()
{
new Y();
}

结果是什么?为什么会是这样呢?
...全文
5332 56 打赏 收藏 转发到动态 举报
写回复
用AI写文章
56 条回复
切换为时间正序
请发表友善的回复…
发表回复
casanova 2001-04-01
  • 打赏
  • 举报
回复
我想我能明白bgsn提这个问题的本意,只是大家不太接受这个题目罢了。不过我相信bgsn没有贬低任何人的意思,只是想和大家讨论一些有可能被忽略的细节问题。我觉得经常这么讨论有助于程序员们的共同进步,而且也是大家来论坛的目的之一吧!
chenyf 2001-04-01
  • 打赏
  • 举报
回复
我想提问者只是想考察一下构造函数中虚函数到底起不起作用的问题。
这东西只需要查手册就知道了,赫赫,还成了热门话题了。
sutao 2001-04-01
  • 打赏
  • 举报
回复
这里的速度太慢了,受不了了.
wb197 2001-04-01
  • 打赏
  • 举报
回复
我试了上面的代码,认为(不告诉你)的代码不能说明虚函数指针有没有改变的问题,因为在Y被构造的时候必然调用X的构造函数,此时还未能对Y类本身的成员函数进行内存分配,当然重载的虚函数也没有被实现,得到上面的结果是必然的喽。而且不能把虚函数的重载放在构造函数里面,构造函数是无法实现重载需函数的,那么我看可能你是无法得到
X::foo()
Y::foo()
这样的结果的。
只是一些个人看法,如果有错敬请指正。
huzhiyan11111 2001-03-30
  • 打赏
  • 举报
回复
hehe
结果是 :
X::foo
X::foo
和我想的一样
muller 2001-03-30
  • 打赏
  • 举报
回复
X::foo()
X::foo()
admireO 2001-03-29
  • 打赏
  • 举报
回复
其实你写的这段代码根本谈不上是虚函数的调用机制的问题.这应该算是一个继承类对象创建时构造函数的调用顺序的问题.因为在构造y对象时,要先调用基类的构造函数,而基类的构造函数调用时肯定于从他继承下来的类y没关系!所以结果必然是:
X::foo()
X::foo()
开始一看你的标题还真有点不自信了,不过从这道题我还是悟出了一个道理;那就是一定要自信;)
cber 2001-03-29
  • 打赏
  • 举报
回复
这个题目确实很难,稍不注意便会疏漏一些重要的东西。而且你只要将这个程序略微的修改一下,又可以发现很多有意思的情况(如用一个inline调用virtual,然后在ctor中调用这个inline)。
希望大家在回答前先仔细的,全面的考虑清楚后再发言。
我很赞同bgsn的话:“不是说真是水平有多高,而是不够谦虚严谨,骄傲自大!!!”
bgsn 2001-03-29
  • 打赏
  • 举报
回复
对不起各位大哥,得罪之处还望多多包涵,其实小弟只是菜鸟一只!!!
Chaoser 2001-03-29
  • 打赏
  • 举报
回复
什么玩艺?标题不要这么吓人好不好?
tzp 2001-03-29
  • 打赏
  • 举报
回复
没过四级看不懂英语呀
wildboar 2001-03-29
  • 打赏
  • 举报
回复
whoa!!!
自己骂自己!!!
bgsn 2001-03-29
  • 打赏
  • 举报
回复
不是说真是水平有多高,而是不够谦虚严谨,骄傲自大!!!
bgsn 2001-03-29
  • 打赏
  • 举报
回复
This behavior makes sense for two reasons. Conceptually, the constructor’s job is to bring the object into existence (which is hardly an ordinary feat). Inside any constructor, the object may only be partially formed – you can only know that the base-class objects have been initialized, but you cannot know which classes are inherited from you. A virtual function call, however, reaches “forward” or “outward” into the inheritance hierarchy. It calls a function in a derived class. If you could do this inside a constructor, you’d be calling a function that might manipulate members that hadn’t been initialized yet, a sure recipe for disaster.
The second reason is a mechanical one. When a constructor is called, one of the first things it does is initialize its VPTR. However, it can only know that it is of the “current” type. The constructor code is completely ignorant of whether or not the object is in the base of another class. When the compiler generates code for that constructor, it generates code for a constructor of that class, not a base class and not a class derived from it (because a class can’t know who inherits it). So the VPTR it uses must be for the VTABLE of that class. The VPTR remains initialized to that VTABLE for the rest of the object’s lifetime unless this isn’t the last constructor call. If a more-derived constructor is called afterwards, that constructor sets the VPTR to its VTABLE, and so on, until the last constructor finishes. The state of the VPTR is determined by the constructor that is called last. This is another reason why the constructors are called in order from base to most-derived.

只想说:中国程序员都TMD太牛X!!!
holyfire 2001-03-29
  • 打赏
  • 举报
回复
呵呵,只想赚点参与分,没别的意思。
holyfire 2001-03-29
  • 打赏
  • 举报
回复
你以为你真的精通 C++ 的皮毛吗 -- 看看上面的问题再说吧!
GEPGP 2001-03-29
  • 打赏
  • 举报
回复
好玩吗?
rh 2001-03-28
  • 打赏
  • 举报
回复
哎……
starcbh 2001-03-28
  • 打赏
  • 举报
回复
不懂就去看 《深入浅出MFC》
KAO!
不难吧!
coldbloodfox 2001-03-28
  • 打赏
  • 举报
回复
靠 这种c++语法我还是第一次看到.......新鲜!
真正懂了再来吧!快被你吓死乐
加载更多回复(36)

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧