C++,构造函数后面的冒号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2785612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++, What does the colon after a constructor mean?
提问by bcoughlan
Possible Duplicates:
Variables After the Colon in a Constructor
C++ constructor syntax question (noob)
I have some C++ code here:
我这里有一些 C++ 代码:
class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)
{
dat = new char[len];
for (int i = 0; i <= le; i++)
dat[i] = default;
}
void ~demo(void)
{
delete [] *dat;
}
};
class newdemo : public demo
{
private:
int *dat1;
public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;
return 0;
}
};
My question is, what are the : len(le)
and : demo(0, 0)
called?
我的问题是,什么是: len(le)
和: demo(0, 0)
叫什么?
Is it something to do with inheritance?
跟继承有关系吗?
回答by Smashery
As others have said, it's an initialisation list. You can use it for two things:
正如其他人所说,这是一个初始化列表。你可以用它做两件事:
- Calling base class constructors
- Initialising member variables before the body of the constructor executes.
- 调用基类构造函数
- 在构造函数体执行之前初始化成员变量。
For case #1, I assume you understand inheritance (if that's not the case, let me know in the comments). So you are simply calling the constructor of your base class.
对于案例#1,我假设您了解继承(如果不是这种情况,请在评论中告诉我)。所以你只是调用基类的构造函数。
For case #2, the question may be asked: "Why not just initialise it in the body of the constructor?" The importance of the initialisation lists is particularly evident for const
members. For instance, take a look at this situation, where I want to initialise m_val
based on the constructor parameter:
对于案例#2,可能会问这个问题:“为什么不在构造函数体中初始化它?” 初始化列表的重要性对于const
成员来说尤为明显。例如,看看这种情况,我想m_val
根据构造函数参数进行初始化:
class Demo
{
Demo(int& val)
{
m_val = val;
}
private:
const int& m_val;
};
By the C++ specification, this is illegal. We cannot change the value of a const
variable in the constructor, because it is marked as const. So you can use the initialisation list:
根据 C++ 规范,这是非法的。我们不能const
在构造函数中更改变量的值,因为它被标记为 const。所以你可以使用初始化列表:
class Demo
{
Demo(int& val) : m_val(val)
{
}
private:
const int& m_val;
};
That is the only time that you can change a const member variable. And as Michael noted in the comments section, it is also the only way to initialise a reference that is a class member.
那是您可以更改 const 成员变量的唯一时间。正如迈克尔在评论部分指出的那样,这也是初始化作为类成员的引用的唯一方法。
Outside of using it to initialise const
member variables, it seems to have been generally accepted as "the way" of initialising variables, so it's clear to other programmers reading your code.
除了使用它来初始化const
成员变量之外,它似乎已被普遍接受为初始化变量的“方式”,因此其他程序员阅读您的代码时很清楚。
回答by dbyrne
This is called an initialization list. It is for passing arguments to the constructor of a parent class. Here is a good link explaining it: Initialization Lists in C++
这称为初始化列表。它用于将参数传递给父类的构造函数。这是解释它的一个很好的链接:C++ 中的初始化列表
回答by Ken Bloom
It's called an initialization list. An initializer list is how you pass arguments to your member variables' constructors and for passing arguments to the parent class's constructor.
它被称为初始化列表。初始值设定项列表是将参数传递给成员变量的构造函数以及将参数传递给父类的构造函数的方式。
If you use =
to assign in the constructor body, first the default constructor is called, then the assignment operator is called. This is a bit wasteful, and sometimes there's no equivalent assignment operator.
如果=
在构造函数体中使用赋值,则首先调用默认构造函数,然后调用赋值运算符。这有点浪费,有时没有等效的赋值运算符。
回答by Chris Schmich
It's called an initialization list. It initializes members before the body of the constructor executes.
它被称为初始化列表。它在构造函数体执行之前初始化成员。
回答by wheaties
It means that len
is not set using the default constructor. while the demo
class is being constructed. For instance:
这意味着len
未使用默认构造函数设置。在demo
构建类时。例如:
class Demo{
int foo;
public:
Demo(){ foo = 1;}
};
Would first place a value in foo before setting it to 1. It's slightly faster and more efficient.
会先在 foo 中放置一个值,然后再将其设置为 1。它稍微快一点,效率也更高。
回答by Chris O
You are calling the constructor of its base class, demo.
您正在调用其基类 demo 的构造函数。