什么时候需要默认的构造函数? [英] When a default constructor necessary?

查看:117
本文介绍了什么时候需要默认的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个类中提供非默认构造函数时,默认构造函数

不再可用。


在什么情况下默认构造函数应该是明确定义了吗?


具体来说,在下列情况下,默认构造函数是否应该提供


1.继承。 A类派生自B类。默认构造函数是否为A和B定义



2.虚拟继承。


3.当A类包含B类时,应该为

A和B定义默认构造函数?


4.抽象基数课程。


提前致谢!

解决方案

* highli:


在下列情况下,是否应提供默认构造函数?

1.继承。 A类是从B类派生出来的。默认构造函数是为A和B定义的吗?

2.虚拟继承。

3.当A类包含时B类,是否应为
A和B定义默认构造函数?

4.抽象基类。




那些可能的原因是_unrelated_是否应该定义默认构造函数



当存在自然默认状态时定义默认构造函数。

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?




" highli" <喜**** @ cls.com>在消息中写道

新闻:lD ******************** @ bgtnsc05-news.ops.worldnet.att.net ... < blockquote class =post_quotes>当一个类中提供的非默认构造函数时,默认的
构造函数不再可用。

在什么情况下应该显式定义默认构造函数?

具体来说,在下列情况下,是否应提供默认构造函数?

1.继承。 A类是从B类派生的。应该为A和B定义一个默认的
构造函数吗?

2.虚拟继承。

3.当A类包含时B类,默认构造函数是否定义为A和B的


4.抽象基类。

提前致谢!




这些都绝对不需要默认构造函数,假设你已经定义了默认构造函数。你可以提供一个替代的

构造函数来满足所有上述条件,就像你一样

可能混合默认构造函数和替代cstors来生成结束

产品。


重要的是你的派生类指出哪个cstor适合在其cstor的初始化列表中创建base / members。其中

是关键的,因为在调用派生类的cstor之前,基类和成员类需要生成
。如果你没有倾倒基础,你就不能建造房子。


如果你问的是是否要求定义cstors ,

的答案是否定的,但是由于C ++赋予了定义创建的权力,为什么不采取

收费并控制流程,因为这可能有助于

跟踪/调试?更不用说你只能通过显式调用得到你想要的东西




顺便提一下,你可能想要花更多时间,是否什么

复制cstor,以及你可能需要的赋值运算符。查看

主题为深层复制。


2004年7月24日星期六22:44:33 GMT,highli< hi****@cls.com>写道:

当一个类中提供非默认构造函数时,默认的
构造函数
不再可用。

什么情况下应该显式定义默认构造函数?

当你希望默认构造函数做一些不同于

编译器生成的默认构造函数或者你想要的时候默认的

构造函数和编译器由于某种原因不会为你生成一个。

具体来说,在下列情况下,默认构造函数应该是
提供?

1.继承。 A类是从B类派生出来的。默认的构造函数是为A和B定义的吗?

2.虚拟继承。

3。当A类包含B类时,是否要定义一个默认构造函数
对于A和B?

4.抽象基类。




这些问题都没有我能看到的任何相关问题。


john


When a non-default constructor provided in a class, the default constructor
is not available anymore.

In what cases shall a default constructor be defined explicitly?

Specifically, in the following cases, shall a default constructor be
provided?
1. Inheritance. Class A is derived from class B. Shall a default constructor
be defined for A and B?

2. Virtual inheritance.

3. When class A contains class B, Shall a default constructor be defined for
A and B?

4. Abstract base class.

Thanks in advance!


解决方案

* highli:


in the following cases, shall a default constructor be provided?

1. Inheritance. Class A is derived from class B. Shall a default constructor
be defined for A and B?

2. Virtual inheritance.

3. When class A contains class B, Shall a default constructor be defined for
A and B?

4. Abstract base class.



Those possible reasons are _unrelated_ to whether a default constructor
should be defined.

Define a default constructor when there is natural default state.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"highli" <hi****@cls.com> wrote in message
news:lD********************@bgtnsc05-news.ops.worldnet.att.net...

When a non-default constructor provided in a class, the default constructor is not available anymore.

In what cases shall a default constructor be defined explicitly?

Specifically, in the following cases, shall a default constructor be
provided?
1. Inheritance. Class A is derived from class B. Shall a default constructor be defined for A and B?

2. Virtual inheritance.

3. When class A contains class B, Shall a default constructor be defined for A and B?

4. Abstract base class.

Thanks in advance!



None of these absolutely require a default constructor assuming that you
have defined what a default constructor is. You can provide an alternate
constructor to satisfy all the above mentioned conditions just like you
might mix default constructors and alternate cstors to generate the end
product.

Whats important is that your derived class indicate which cstor is
appropriate to create base/members in its cstor''s initialization list. Which
is critical since the base class(es) and member class(es) need to be
generated before the cstor of the derived class is invoked. You can''t build
the house if you haven''t poured the foundation.

If what you are asking is whether defining cstors are a requirement, the
answer is no, but since C++ gives the power to define creation, why not take
charge and control the process since that may help in the case of
tracing/debugging? Not to mention the fact that you only get what you want
with explicit invocations.

Incidentally, what you might want to spend more time on, is whether and what
copy cstor, as well as assignment operator you might need. Look up the
subject of "deep copy".


On Sat, 24 Jul 2004 22:44:33 GMT, highli <hi****@cls.com> wrote:

When a non-default constructor provided in a class, the default
constructor
is not available anymore.

In what cases shall a default constructor be defined explicitly?

When you want the default constructor to do something different from the
compiler generated default constructor or when you want a default
constructor and the compiler won''t generate one for you for some reason.
Specifically, in the following cases, shall a default constructor be
provided?
1. Inheritance. Class A is derived from class B. Shall a default
constructor
be defined for A and B?

2. Virtual inheritance.

3. When class A contains class B, Shall a default constructor be defined
for
A and B?

4. Abstract base class.



None of these issues are relevant in any way that I can see.

john


这篇关于什么时候需要默认的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆