-2

I wish to have an object of one class contained inside a different class.

class A contained inside class B as in the simple example below.

The problem is that class A only has a parametrized constructor.

Is there another way to do declare the class A object in class B without having to use a pointer to class A ?

class A
{
    public:
        A(int var1, int var2);
    private:
        //...
};

class B
{
    public:
        B();
    private:
        A  a;      // Compiler error 
        A* a_ptr; // This will of course work fine. We can create a new A object with parameters any time using the a_ptr
};
Engineer999
  • 3,683
  • 6
  • 33
  • 71
  • I guess this compiler error is self sufficient to understand what you can do in this case: ---constructor for 'B' must explicitly initialize the member 'a' which does not have a default constructor – Abhishek Chandel Oct 18 '20 at 09:00
  • Unfortunately, you are misrepresenting or misinterpreting your problem. `B` having a member of type `A` doesn't cause a compilation error, unless the definition of `A` is not visible to the compiler (e.g. from a previously included header). A compilation error will be the result if the constructor of `B` attempts to construct an `A` using a constructor that `A` does not have. Change the definition of `B`s constructor to (assuming not inline in the definition of class `B`) to `B::B() : A (2,3) {}` and it will compile. (I've picked values of `2` and `3` randomly to make the point). – Peter Oct 18 '20 at 09:02
  • @Peter you mean `B::B() : a (2,3) {}` (*a* not *A*) because *B* does not inherits *A* and you want to give arguments to constructor of *A* for *a* – bruno Oct 18 '20 at 09:10

1 Answers1

0

A a; // Compiler error

except if the constructor of B explicitly calls the constructor of A with required arguments (e.g. B() : a(an_int, an_int) {...}), that requests a constructor without parameter or where all parameters have default value, but you do not have that constructor, only a constructor requiring 2 int

Is there another way to do declare the class A object in class B without having to use a pointer to class A ?

you do not declare but define / instantiate a new instance of A each time you instantiate a new instance of B

Note you can create later the instance of A without using a pointer as with a_ptr having for instance

std::vector<A> a;

and when you know the instance of A you need doing for instance

a.push_back(A(an-int, an-int));

but the fact you want only one instance is not visible in the definition of a (out of a welcome comment)

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1
    why DVs ? can you help me to understand them ? – bruno Oct 18 '20 at 09:14
  • Of course, I understand the compiler error. It's trying to create an object of class A by using a non-parametrized constructor which doesn't exist. How can I do this without having to use pointers? I can write A a(int var1, int var2); Inside class B – Engineer999 Oct 18 '20 at 09:26
  • @Engineer999 as I say in my answer you need to explicitly calls the only existing constructor of *A* in *B* constructor. You cannot declare/define *A* constructor in class *B* – bruno Oct 18 '20 at 09:28
  • I can use initializer lists with the constructor of class B to initialize the object A a with parameters. This works. However, if I don't know the values to pass to the class A constructor at the time of class B's construction, then it looks like I have no choice but to use pointers? – Engineer999 Oct 18 '20 at 09:32
  • @Engineer999 while you don't know the values to pass yes you cannot instantiate the class, but you can use a vector for instance to create it later, I edited my answer – bruno Oct 18 '20 at 09:38
  • @Engineer999 is my answer ok for you ? – bruno Oct 19 '20 at 17:33