简体   繁体   English

没有默认构造函数的类成员

[英]Class member without a default constructor

Suppose I have a class A without a default constructor, a factory method factoryA that returns an object of type A, and a class B that has A as its member. 假设我有一个没有默认构造函数的类A,一个返回类型A的对象的工厂方法factoryA,以及一个以A作为其成员的类B. I know that in this case the member of type A of B has to be initialize in B's constructor initialization list. 我知道在这种情况下,B类型的成员必须在B的构造函数初始化列表中初始化。 It is not entirely clear to me why so if someone could explain that to me it would be great. 我并不完全清楚为什么如果有人可以向我解释这将是伟大的。 Also, what if the parameter to A's constructor needs to be computed inside of B's constructor, say by querying a database or something of that nature? 另外,如果A的构造函数的参数需要在B的构造函数内部计算,比如通过查询数据库或者那种性质的东西,该怎么办? Is there a way to use the setup below without providing A with a default constructor? 有没有办法使用下面的设置,而不提供A与默认构造函数? Thanks in advance. 提前致谢。

class A {
private:
  int _i;
public:
  A(int i) : _i(i) {} 
};

A factoryA(bool b) {
  if(b)
    return A(1);
  else return A(2);
}

class B {
private:
  A _a;
public:
  B(int j) {
    if(j > 0)
      _a = factoryA(true);
    else _a = factoryA(false);
  }
};

Member objects are always initialized before entry into the body (the part between the curly braces) of the constructor. 在进入构造函数的主体(花括号之间的部分)之前,始终会初始化成员对象。 If you don't mention a member in the initializer list, it gets default constructed. 如果您没有在初始化列表中提及成员,则会默认构造它。 So mention it! 所以提一下吧!

B::B(int j) : _a(factoryA(0 < j)) { };

This calls the function factoryA with the argument value true if j is greater than 0 and false otherwise, and initializes the member _a with the value returned by that call. 如果j大于0,则调用函数factoryA ,参数值为true ,否则调用false ,并使用该调用返回的值初始化成员_a

It is not entirely clear to me why so if someone could explain that to me it would be great. 我并不完全清楚为什么如果有人可以向我解释这将是伟大的。

For classes[*], the _a = factoryA(true); 对于类[*], _a = factoryA(true); line calls _a.operator=(factoryA(true)) . 行调用_a.operator=(factoryA(true)) Calling a member function on _a requires _a to already be initialised. _a上调用成员函数需要_a已经初始化。 So if it weren't a compile-time error, it still wouldn't be valid. 因此,如果它不是编译时错误,它仍然无效。

Also, what if the parameter to A's constructor needs to be computed inside of B's constructor, say by querying a database or something of that nature? 另外,如果A的构造函数的参数需要在B的构造函数内部计算,比如通过查询数据库或者那种性质的东西,该怎么办? Is there a way to use the setup below without providing A with a default constructor? 有没有办法使用下面的设置,而不提供A与默认构造函数?

As long as A has a copy or move constructor, you can initialise it with a function return value, and that function can do anything you want, even using different constructors for A depending on the arguments provided. 只要A具有复制或移动构造函数,就可以使用函数返回值对其进行初始化,并且该函数可以执行任何您想要的操作,甚至根据提供的参数使用不同的A构造函数。

class B {
private:
  A _a;
  static A getA(int i);
public:
  B(int j) : _a(getA(j)) {
  }
};

A B::getA(int j)
{
  if (j > 0)
    return factoryA(true);
  else
    return factoryA(false);
}

[*] I know, there are exceptions. [*]我知道,也有例外。

在这种情况下,最好使用指向A的指针,即A * _a,然后在任何地方调用A构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM