在构造函数中调用类成员的构造函数

时间:2023-01-15 16:47:03

Can I call constructor of a member in my Class's constructor?

我可以在Class的构造函数中调用成员的构造函数吗?

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?

如果我在类MClass中有一个类型为foo的成员栏。我可以在MClass的构造函数中调用bar的构造函数吗?如果没有,那么我如何初始化我的会员栏?

It is a problem of initializing members in composition(aggregation).

这是在合成(聚合)中初始化成员的问题。

5 个解决方案

#1


30  

Yes, certainly you can! That's what the constructor initializer list is 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.

您还需要初始化列表来为派生类构造函数中的基类指定非默认构造函数。

#2


6  

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

View this on ideone.com

在ideone.com上查看此内容

#3


3  

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.

请记住:成员按照它们在类中声明的顺序进行初始化,而不是它们在初始化列表中出现的顺序。

#4


3  

Through initializer list, if base class doesn't have a default constructor.

通过初始化列表,如果基类没有默认构造函数。

struct foo{
   foo( int num )
   {}
};

struct bar : foo {
   bar( int x ) : foo(x)
               // ^^^^^^ initializer list
   {}
};

#5


2  

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。通常,参数将是您的类的其他成员或传递给构造函数的参数。任何没有无参数构造函数的成员都需要此语法。

#1


30  

Yes, certainly you can! That's what the constructor initializer list is 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.

您还需要初始化列表来为派生类构造函数中的基类指定非默认构造函数。

#2


6  

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

View this on ideone.com

在ideone.com上查看此内容

#3


3  

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.

请记住:成员按照它们在类中声明的顺序进行初始化,而不是它们在初始化列表中出现的顺序。

#4


3  

Through initializer list, if base class doesn't have a default constructor.

通过初始化列表,如果基类没有默认构造函数。

struct foo{
   foo( int num )
   {}
};

struct bar : foo {
   bar( int x ) : foo(x)
               // ^^^^^^ initializer list
   {}
};

#5


2  

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。通常,参数将是您的类的其他成员或传递给构造函数的参数。任何没有无参数构造函数的成员都需要此语法。