找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1267|回复: 7

[求助]构造函数为什么不可以为虚成员函数

[复制链接]
发表于 2009-11-3 02:56:25 | 显示全部楼层 |阅读模式 IP:江苏扬州
构造函数为什么不可以为虚成员函数 想不来~
发表于 2009-11-3 02:56:26 | 显示全部楼层 IP:江苏扬州
官方的解释大致是:
虚拟调用是一种能够在给定信息不完全(given partial information)的情况下工作的机制。特别地,虚拟允许我们调用某个函数,对于这个函数,仅仅知道它的接口,而不知道具体的对象类型。但是要建立一个对象,你必须拥有完全的信息。特别地,你需要知道要建立的对象的具体类型。因此,对构造函数的调用不可能是虚拟的。
我的加一点自己额外的理解:
用c++语言对虚拟函数的实现方面来理解,根据虚成员函数的定义,假设构造函数是虚函数的话,调用虚成员构造函数来构造对象,首先需要虚函数指针表来获得虚拟构造函数地址,虚函数指针表的地址又是存放在对象中,但是现在对象还不存在,等着用虚拟构造函数来创建呢,所以......
回复

使用道具 举报

发表于 2009-11-3 02:56:28 | 显示全部楼层 IP:江苏扬州
C++编程思想中的解释

The hierarchy of constructor calls brings up an interesting dilemma. What happens if you’re inside a constructor and you call a virtual function? Inside an ordinary member function you can imagine what will happen – the virtual call is resolved at runtime because the object cannot know whether it belongs to the class the member function is in, or some class derived from it. For consistency, you might think this is what should happen inside constructors.

This is not the case. If you call a virtual function inside a constructor, only the local version of the function is used. That is, the virtual mechanism doesn’t work within the constructor.

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 type the constructor was written for. 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.

But while all this series of constructor calls is taking place, each constructor has set the VPTR to its own VTABLE. If it uses the virtual mechanism for function calls, it will produce only a call through its own VTABLE, not the most-derived VTABLE (as would be the case after all the constructors were called). In addition, many compilers recognize that a virtual function call is being made inside a constructor, and perform early binding because they know that late-binding will produce a call only to the local function. In either event, you won’t get the results you might initially expect from a virtual function call inside a constructor.

You cannot use the virtual keyword with constructors, but destructors can and often must be virtual.
回复

使用道具 举报

发表于 2009-11-3 02:56:29 | 显示全部楼层 IP:江苏扬州
呵呵
说的这么高难呢??
构造函数是来创建一个对象
而虚函数只对对象起作用
对象还没创建呢 你怎么用呢
回复

使用道具 举报

发表于 2009-11-3 02:56:30 | 显示全部楼层 IP:江苏扬州
这段英文是在解释这句
If you call a virtual function inside a constructor, only the local version of the function is used.

You cannot use the virtual keyword with constructors
的原因 song4已经说的很清楚了
回复

使用道具 举报

发表于 2009-11-3 02:56:32 | 显示全部楼层 IP:江苏扬州
构造函数为什么不可以为虚成员函数



构造函数不能说明为虚函数,因为对象在创建时它还没有确定内存空间,只有在构造后才有一个类的实例.
回复

使用道具 举报

发表于 2009-11-3 02:56:33 | 显示全部楼层 IP:江苏扬州
以前很模糊现在很清楚了 谢谢各位高手了^^
回复

使用道具 举报

发表于 2009-11-3 02:56:34 | 显示全部楼层 IP:江苏扬州
3楼的解释不尽正确。不是因为constructor中vtable还没有完全建立所以不能用polymorphism(多态)。而是在函数内部根本就不应该多态。virtual 只对reference和pointer起作用。这里显然不是这种情况。按C++ Programming language里的解释:
A type with virtual functions is called a polymorphic type. To get polymorphic behavior in C++, the member functions called must be virtual and objects must be maniplated through pointers or references.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 11:24 , Processed in 0.203462 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表