简体   繁体   English

C ++如何初始化成员对象?

[英]C++ How to Initialize member object?

Is there some way to initialize(?) member variable c of type C in the first part of the below example? 在下面的示例的第一部分中,是否有某种方法可以初始化C类型的成员变量c? Or must I use the new() method shown in the second part of the example? 还是必须使用示例第二部分中显示的new()方法?

Class B takes class A as an injected dependency. B类将A类作为注入依赖项。 So does class C. Class B is additionally composed of class C. C类也是如此。B类另外由C类组成。

How do I get the injected A to B's member C? 我如何获得注入的A给B的成员C?

Part 1 第1部分

class A { // ...; };

class C {
public:
    C(A &a) : a(a) {}   // constructor
};


// Does not work as is.  Want to make compiler manage C lifetime.
class B {
public:
    B(A &a);    // constructor

    C c(a);     // member variable
};

// constructor
B::B(A &a) : a(a) {
}

Part 2 第2部分

// Works, but requires programmer to manage C's lifetime.
class B {
public:
    B(A &a);    // constructor

    C *c;       // member variable
};

// constructor
B::B(A &a) : a(a) {
    c = new C(a);
}

Several good answers below! 以下是几个不错的答案! My apologies for the confusing example. 对于令人困惑的示例,我深表歉意。 I have up-voted all of the good answers and questions. 我对所有好的答案和问题都投票赞成。 Unfortunately I can only mark one answer as the accepted answer, so I am choosing the first one which gave me the "ah-ha" moment in which I saw the solution to my real problem, which was more complex than my lame example here. 不幸的是,我只能将一个答案标记为已接受的答案,因此我选择的第一个答案是“ ah-ha”,在那一刻中,我看到了实际问题的解决方案,这比此处的la脚示例更为复杂。

Member variables are initialized in constructor's initialization list (before body) so you need to do that: 成员变量在构造函数的初始化列表(在主体之前)中初始化,因此您需要执行以下操作:

B::B(A &a)
    : c(a) // Calls constructor C(a) on member c
{}

You almost have it: 您几乎拥有它:

class B {
public:
    B(A &a);

    C c(a); //see note 1
};

B::B(A &a) : a(a) { //see note 2
}

Note 1: 注1:

There are two problems with C c(a); C c(a);有两个问题C c(a); here: 这里:

  1. a is not in scope. a不在范围内。 a only exists within the scope of the constructor, so c needs to be initialized from there. a仅存在于构造函数的范围内,因此c需要从那里初始化。
  2. Until C++11, non-static data member initializers (NSDMIs) were prohibited. 在C ++ 11之前,禁止使用非静态数据成员初始化器(NSDMI)。 Even in C++11, though, you must use an equals sign ( C c = value; ) or braces ( C c{value}; ) when initializing an NSDMI. 但是,即使在C ++ 11中,初始化NSDMI时也必须使用等号( C c = value; )或大括号( C c{value}; )。

Note 2: 笔记2:

You've almost got this right: 您几乎完全正确:

B::B(A &a) : a(a)

You're trying to initialize a data member called a with the argument given to the constructor. 您正在尝试使用提供给构造函数的参数来初始化名为a的数据成员。 You actually want to initialize c like this, not a non-existent a : 您实际上想要这样初始化c ,而不是不存在的a

B::B(A &a) : c(a)

The lifetime of c will be that of the instance of the B class. c的生存期将是B类实例的生存期。 Using dynamic memory management is certainly not necessary. 使用动态内存管理当然不是必需的。

"How do I get the injected A to B's member C?" “我如何将注入的A交给B的成员C?”

You can do so using B 's constructor member initializer list 您可以使用B的构造函数成员初始化器列表进行操作

class B {
public:
    B(A &a) : c(a) {
           // ^^^^
    }

    C c; // <<< It's not possible to initialize members in their
         //     declaration.
};

The following: 下列:

C(A &a) : a(a) {}

will not compile since a (the first a in the initialization list) is not a member variable of C . 将不会编译,因为a (初始化列表中的第a )不是C的成员变量。

Same applies to the constructor of B . 同样适用于B的构造函数。

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

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