免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: dream-girl
打印 上一主题 下一主题

[函数] 构造函数? [复制链接]

论坛徽章:
0
41 [报告]
发表于 2004-12-27 19:42 |只看该作者

构造函数?

如果你构造了几个构造函数,那么你实例化的时候就需要匹配其中的一个,如果没有按照默认的类型转换,继续匹配,如果只能匹配一个那么她不会出错,如果没有找到匹配的,或者找到2个可以匹配的,那么就会产生二义性,都是错误的.

论坛徽章:
0
42 [报告]
发表于 2004-12-27 19:44 |只看该作者

构造函数?

inside the c++ object mode里面说得很清楚

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
43 [报告]
发表于 2004-12-27 20:03 |只看该作者

构造函数?

[quote]原帖由 "playmud"]如果你自己定义了构造函数,那么编译器将不再给你生成默认的构造函数,一般来讲编译器给你做的构造函数基本上没什么用.[/quote 发表:


这句话就不对了,编译器合成的构造函数,1 负责调用基类的缺省构造函数(如果基类有缺省构造函数的话),2 负责正确初始化虚函数表的指针,并添加进类的相应域中。
所以不是没有用,而是十分有用。

论坛徽章:
0
44 [报告]
发表于 2004-12-27 20:09 |只看该作者

构造函数?

原帖由 "gvim" 发表:


这句话就不对了,编译器合成的构造函数,1 负责调用基类的缺省构造函数(如果基类有缺省构造函数的话),2 负责正确初始化虚函数表的指针,并添加进类的相应域中。
所以不是没有用,而是十分有用。

构造函数是不会被继承的,你说的1我觉得不对,你子类没有构造函数,你实例化的时候一样基类先构造,并不是子类的默认构造函数调用的.2,初始化虚函数的指针不是构造函数的事情,这个可以作一个试验,什么时候构造函数被调用呢?当data member被初始化以后,vptr被赋值指向虚函数表(如果有虚函数的话),然后才开始调用构造函数.

论坛徽章:
0
45 [报告]
发表于 2004-12-27 20:11 |只看该作者

构造函数?

如果如你所说的话,在构造函数里面调用虚函数他将不会知道这个虚函数是那里的.

论坛徽章:
0
46 [报告]
发表于 2004-12-27 20:18 |只看该作者

构造函数?

原帖由 "kj501" 发表:

要完整的看待这句话。这名话的意思 是说在定义了构造函数同时又没有定义缺省构造函数的情况下,编译器才会出错。也就是说,只要你定义了一个构造函数,就要同时定义缺省构造函数才行。

认真把这部分内容再看了一遍,发现自己确实是片面地理解了《Thinking in C++》中的这段话:
class V {
int i; // private
}; // No constructor

int main() {
V v, v2[10];
} ///:~

If any constructors are defined, however, and there’s no default constructor, the instances of V above will generate compile-time errors.

它指的是在上面这个例子类V中如果定义了构造函数并且没有缺省构造函数时,在这种情况下,下面这句将会调用缺省构造函数的语句:

  1. V v, v2[10];
复制代码

将由于找不到缺省构造函数而编译器又不提供一个缺省构造函数而报错。
可见它的这句话是针对所举的例子来讲的,而我片面地把它理解成了针对一般情况。从这个角度理解,书上讲的实际上和playmud与gvim的说法并不矛盾。只是自己很少用C++,学完语法后,水平一直都没有什么长进。看来以后看书时确实要细心。
最后,非常感谢playmud和gvim的热心指正。

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
47 [报告]
发表于 2004-12-27 20:20 |只看该作者

构造函数?

相信你也有那本书,你去看看再说,我记得是“构造函数语义学”那章。
ok?

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
48 [报告]
发表于 2004-12-27 20:27 |只看该作者

构造函数?

kj501当我看到那段E文的时候,理解和你一样。呵呵,后来才发现那个V其实是特指的代码段里面的那个V而不是泛指的“类”

论坛徽章:
0
49 [报告]
发表于 2004-12-27 20:28 |只看该作者

构造函数?

看看书上怎么说得,俺英语不好:
Constructors can contain a great deal of hidden program code because the compiler augments every constructor to a greater or lesser extent depending on the complexity of T's class hierarchy. The general sequence of compiler augmentations is as follows:

The data members initialized in the member initialization list have to be entered within the body of the constructor in the order of member declaration.

If a member class object is not present in the member initialization list but has an associated default constructor, that default constructor must be invoked.

Prior to that, if there is a virtual table pointer (or pointers) contained within the class object, it (they) must be initialized with the address of the appropriate virtual table(s).

Prior to that, all immediate base class constructors must be invoked in the order of base class declaration (the order within the member initialization list is not relevant).

If the base class is listed within the member initialization list, the explicit arguments, if any, must be passed.

If the base class is not listed within the member initialization list, the default constructor (or default memberwise copy constructor) must be invoked, if present.

If the base class is a second or subsequent base class, the this pointer must be adjusted.

Prior to that, all virtual base class constructors must be invoked in a left-to-right, depth-first search of the inheritance hierarchy defined by the derived class.

If the class is listed within the member initialization list, the explicit arguments, if any, must be passed. Otherwise, if there is a default constructor associated with the class, it must be invoked.

In addition, the offset of each virtual base class subobject within the class must somehow be made accessible at runtime.

These constructors, however, may be invoked if, and only if, the class object represents the "most-derived class." Some mechanism supporting this must be put into place.

In this section, I consider the augmentations necessary to constructors in order to support the language-guaranteed semantics of the class. I again illustrate the discussion with the help of a Point class. (I added a copy constructor, copy operator, and virtual destructor in order to illustrate the behavior of subsequent containing and derived classes in the presence of these functions.)

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
50 [报告]
发表于 2004-12-27 20:41 |只看该作者

构造函数?

原帖由 "playmud" 发表:

当data member被初始化以后,vptr被赋值指向虚函数表(如果有虚函数的话),然后才开始调用构造函数.


老兄,你英文理解错了。
“Prior to that”是“在那之前”的意思。that指代的是上一个动作。你完全理解反了。文章的大概意思是:初始化data member,如果有成员类对象(没有列在data member list中)那么这个成员类对象的缺省构造函数需要被调用(如果这个成员对象有缺省构造函数)。在那之前,(什么之前?就是指上面的行为)如果有虚函数表指针…………
剩下的自己翻译吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP