I have a class with a hidden default constructor to force the use of a constructor taking parameters. Another class is using 2 instances of the class:
typedef struct { ... } BAZ;
class Foo {
private:
Foo(void) {}
public:
Foo(BAZ a) { ... }
};
class Bar {
private:
Foo foo1;
Foo foo2;
Bar(void) {}
public:
Bar(BAZ a, BAZ b) : foo1(a), foo2(b) { ... }
};
The most obvious is that the declaration of the variables foo1 and foo2 will call the default Foo constructor, but since it is private it can't and will give a compiler error.
Is there a way to prevent it from trying the default Foo constructor and just wait for the Bar constructor to initialise them instead?
I want to avoid the use of the new keyword (which would solve the whole problem).
EDIT:
It seems like people have a hard time to understand the question and dilemma. I will try to explain:
I want to force the use of a Foo(BAZ) constructor meaning that any attempt to use the Foo(void) constructor will generate an error.
To hide the default constructor it is declared as a private member. If someone try to use the default constructor Foo() it will give an intentional error.
To not declare the default constructor and only declare Foo(BAZ) is not preventing the compiler to create a public default constructor. It gives no error if I declare it Foo(). So far it works fine and as intended.
The second class Bar have two instances of Foo but when Bar is instanced these Foo members will be called with the default (hidden) constructor and generate an error. Then in the Bar constructor, these two instances will be initialized with the correct public constructor Bar(BAZ a, BAZ b) : foo1(a), foo2(b). This is what I want.
Is there a way to prevent it from calling Foo default constructor when Bar is initialized, so the Bar constructor can use the correct Foo constructor?
The new solution works because the default constructor is never called:
BAZ a = {...}
Foo *foo1 = new Foo(a);
I hope this makes it more clear.
EDIT2: SOLVED The error wasn't in the hidden Foo constructor, it was the hidden Bar constructor trying to use the hidden default Foo constructors.
Bar(void) : Foo(BAZ{}), Foo(BAZ{}) {}
Solved it.
EDIT3:
The real problem seems to have been in the development tool. After restart and manually clearing the cache it worked as the C++14 standard intended.