Is there a way to initialize (?) A member variable c of type C in the first part of the example below? Or should I use the new () method shown in the second part of the example?
Class B takes class A as an embedded dependency. Also class C. Class B additionally consists of class C.
How to get the member C entered from A to B?
Part 1
class A {
class C {
public:
C(A &a) : a(a) {}
};
class B {
public:
B(A &a);
C c(a);
};
B::B(A &a) : a(a) {
}
Part 2
class B {
public:
B(A &a);
C *c;
};
B::B(A &a) : a(a) {
c = new C(a);
}
Some good answers below! I apologize for the confusing example. I voted for all the good answers and questions. Unfortunately, I can only mark one answer as an accepted answer, so I choose the first one that gave me “ah ha”, in which I saw a solution to my real problem, which was more complicated than my lame example.
source
share