| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1276 人关注过本帖
标题:[求助]构造函数为什么不可以为虚成员函数
只看楼主 加入收藏
xialup
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-3-20
收藏
 问题点数:0 回复次数:7 
[求助]构造函数为什么不可以为虚成员函数

构造函数为什么不可以为虚成员函数 想不来~

搜索更多相关主题的帖子: 成员函数 构造函数 
2006-06-01 14:17
aogun
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:638
专家分:0
注 册:2006-4-5
收藏
得分:0 
官方的解释大致是:
虚拟调用是一种能够在给定信息不完全(given partial information)的情况下工作的机制。特别地,虚拟允许我们调用某个函数,对于这个函数,仅仅知道它的接口,而不知道具体的对象类型。但是要建立一个对象,你必须拥有完全的信息。特别地,你需要知道要建立的对象的具体类型。因此,对构造函数的调用不可能是虚拟的。
我的加一点自己额外的理解:
用c++语言对虚拟函数的实现方面来理解,根据虚成员函数的定义,假设构造函数是虚函数的话,调用虚成员构造函数来构造对象,首先需要虚函数指针表来获得虚拟构造函数地址,虚函数指针表的地址又是存放在对象中,但是现在对象还不存在,等着用虚拟构造函数来创建呢,所以......

[此贴子已经被作者于2006-6-1 17:00:36编辑过]


世界上总共有 10 种人,一种懂得什么是二进制 ,一种不懂。
2006-06-01 17:00
ooooo
Rank: 1
等 级:新手上路
威 望:1
帖 子:135
专家分:0
注 册:2005-6-24
收藏
得分:0 

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.


2006-06-01 19:06
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
呵呵
说的这么高难呢??
构造函数是来创建一个对象
而虚函数只对对象起作用
对象还没创建呢 你怎么用呢

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-06-01 21:11
ooooo
Rank: 1
等 级:新手上路
威 望:1
帖 子:135
专家分:0
注 册:2005-6-24
收藏
得分:0 
这段英文是在解释这句
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已经说的很清楚了

2006-06-01 21:30
stylev
Rank: 1
等 级:新手上路
威 望:1
帖 子:136
专家分:0
注 册:2006-5-30
收藏
得分:0 

构造函数为什么不可以为虚成员函数


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

E-mail/MSN: stylev@
2006-06-02 02:57
xialup
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-3-20
收藏
得分:0 

以前很模糊现在很清楚了 谢谢各位高手了^^


2006-06-02 09:05
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
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.

[此贴子已经被作者于2006-6-3 4:24:56编辑过]


http://myajax95./
2006-06-03 04:22
快速回复:[求助]构造函数为什么不可以为虚成员函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018744 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved