vendredi 27 janvier 2017

How to call an object constructor inside another constructor?

I have an abstract class A a class B with constructor:

B(A &a) {...}

Now, I create these object in the main program as:

int main(){
  std::unique_ptr<A> a = make_A();//don't mind make_A()
  B b (*a); 
}

Now I created a class which setup the system:

class System{
public:
  System();
private:
  B b;
  std::unique_ptr<A> a;
}

And the constructor is:

System(){
  a = make_A();
  b(*a);
}

Obviously this is wrong because I can't call B's constructor as in the code above, but I can't figure out how to do this. The only solution that came to my mind is to use std::unique_ptr<B> b; instead of B b; and then call b = make<B> (*a);.

Aucun commentaire:

Enregistrer un commentaire