C++ 在构造函数中调用类成员的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7761676/
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
calling constructor of a class member in constructor
提问by shampa
Can I call constructor of a member in my Class's constructor?
我可以在我的类的构造函数中调用成员的构造函数吗?
let say If I have a member bar
of class type foo
in my class MClass
. Can I call constructor of bar in MClass's constructor? If not, then how can I initialize my member bar?
让说,如果我有一个成员bar
类类型的foo
在我的课MClass
。我可以在 MClass 的构造函数中调用 bar 的构造函数吗?如果没有,那么我如何初始化我的会员栏?
It is a problem of initializing members in composition(aggregation).
这是在组合(聚合)中初始化成员的问题。
回答by Kerrek SB
Yes, certainly you can! That's what the constructor initializer listis for. This is an essential feature that you require to initialize members that don't have default constructors, as well as constants and references:
是的,当然可以!这就是构造函数初始值设定项列表的用途。这是初始化没有默认构造函数以及常量和引用的成员所需的基本功能:
class Foo
{
Bar x; // requires Bar::Bar(char) constructor
const int n;
double & q;
public:
Foo(double & a, char b) : x(b), n(42), q(a) { }
// ^^^^^^^^^^^^^^^^^^^
};
You further need the initializer list to specify a non-default constructor for base classes in derived class constructors.
您还需要初始化列表来为派生类构造函数中的基类指定一个非默认构造函数。
回答by elder_george
Yes, you can:
是的你可以:
#include <iostream>
using std::cout;
using std::endl;
class A{
public:
A(){
cout << "parameterless" << endl;
}
A(const char *str){
cout << "Parameter is " << str <<endl;
}
};
class B{
A _argless;
A _withArg;
public:
// note that you need not call argument-less constructor explicitly.
B(): _withArg("42"){
}
};
int main(){
B b;
return 0;
}
The output is:
输出是:
parameterless
Parameter is 42
回答by phs
Like this:
像这样:
class C {
int m;
public:
C(int i):
m(i + 1) {}
};
If your member constructor wants parameters, you can pass them. They can be expressions made from the class constructor parameters and already-initialized types.
如果您的成员构造函数需要参数,您可以传递它们。它们可以是由类构造函数参数和已初始化类型构成的表达式。
Remember: members are initialized in the order they are declared in the class, not the order they appear in the initialization list.
请记住:成员按照它们在类中声明的顺序进行初始化,而不是它们在初始化列表中出现的顺序。
回答by Mahesh
Through initializer list, if base class doesn't have a defaultconstructor.
通过初始化列表,如果基类没有默认构造函数。
struct foo{
foo( int num )
{}
};
struct bar : foo {
bar( int x ) : foo(x)
// ^^^^^^ initializer list
{}
};
回答by Dave S
Yes, you can. This is done in the initialization list of your class. For example:
是的你可以。这是在您的类的初始化列表中完成的。例如:
class MClass
{
foo bar;
public:
MClass(): bar(bar_constructor_arguments) {};
}
This will construct bar
with the arguments passed in. Normally, the arguments will be other members of your class or arguments that were passed to your constructor. This syntax is required for any members that do not have no-argument constructors.
这将bar
使用传入的参数进行构造。通常,参数将是您的类的其他成员或传递给您的构造函数的参数。任何没有无参数构造函数的成员都需要此语法。