此构造函数如何工作 [英] How does this constructor work

查看:77
本文介绍了此构造函数如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在阅读C ++入门书时,我遇到了一个代码,它看起来如下:

Hi,

I have come across a code when I was reading a C++ primer which looks the following:

class MyIntArray {
  MyIntArray(int size = DefaultSize){}
 private:
 const static DefaultSize = 12;
};


我的疑问是:
->在书中提到它是默认的构造函数,但是它怎么有参数呢?
->括号中的代码如何工作?


My doubts are the followng:
->Its mentioned in the book that it is a default constructor,but how can it have an argument?
->How does that code in parenthesis work?

Thaks in advance.

推荐答案

要补充Manfred所说的话,它是默认构造函数:它只是带有可选参数的默认构造函数.如果省略该参数,则它是默认构造函数.不用,它是一个参数化的构造函数.

只要C ++中没有任何非可选参数,C ++中的任何参数都可以是可选参数.您所需要做的就是在函数声明中提供默认值.
MSDN [
To add to what Manfred said, it is a default constructor: it is just a default constructor with a optional argument. If you omit the argument, it is a default constructor. It you don''t, it is a parametrised constructor.

Any parameter in C++ can be optional, provided there are no non-optional parameters to the right of it. All you have to do it provide the default value as part of the function declaration.
MSDN[^] has a reasonable explanation.


IBM:

缺省构造函数是不带参数,或者如果有参数,则所有参数都具有缺省值的构造函数值.

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.



http:///publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr376.htm [



http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr376.htm[^]


虽然您的问题已在上面得到了彻底的回答,但我想指出的是,如果此代码是您正在阅读的该引物的文字记录,最好立即丢弃它,并获得一些不错的书或其他在线资源:

1.您不能在类定义内直接定义const值.如果该定义位于标头中,并且通常可以从多个位置包含该标头.这样定义,您将获得链接器错误.

2.不仅将const定义,而且将构造函数定义为私有.我怀疑那是意图.

3.我希望构造函数使用参数进行一些琐碎的初始化.相反,它不执行任何操作,从而引发了有关该代码示例的有效性和目的的疑问.

4.我希望有一个不错的入门手册来解释如果在示例代码中使用默认参数,则默认参数是什么.从您的问题来看,不是.

我并不是希望从一个简单的入门文章中全面了解C ++,但是提供甚至不进行编译和链接的示例似乎是错误的.如果解释是完全错误的,您如何对C ++的功能和功能有良好的印象?
While your questions have been thoroughly answered above, I''d like to point out that if this code is a literal transcript of that primer you are reading, you''d best discard it at once and get some decent book or other online source:

1. You can''t define a const value right inside the class definition. If that definition is inside a header and that header may be included from multiple locations, as is normally the case. Defined like that, you''ll get linker errors.

2. Not only the const, but also the constructor is defined private. I doubt that was the intention.

3. I would expect the constructor to do some trivial initialization, using the argument. Instead it does nothing, raising questions about the validity and purpose of that code example.

4. I''d expect a decent primer to explain what default arguments are if it uses them in example code. Judging by your question it doesn''t.

It''s not that I''m expecting a full-fledged introduction into C++ from a simple primer, but it seems wrong to provide examples that wouldn''t even compile & link. How can you get a good impression of the functionality and capabilities of C++ if the explanations are plain wrong?


这篇关于此构造函数如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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