For a project I am looking at a case where the following situation exists. There is a class foo which requires an identifying number x when constructed. So its constructor argument looks something like foo(x).
Now I want to derive a class based on foo, called bar, which will basically hold two foo's internally and to the outside world acts as a single foo. The way I want to do this is by use of two constructor arguments y and z for bar, which will then, in the constructor, generate foo(y) and foo(z). I hope it's clear.
My current implementation looks something like:
class bar : public foo {
public:
bar(int y, int z)
{
_foo_ptr_1 = foo::foo(y);
_foo_ptr_2 = foo::foo(z);
};
private:
foo *_foo_ptr_1;
foo *_foo_ptr_2;
};
Where foo is a class that is an already existing class looking something like:
class foo {
public:
foo(int x) :
_private_var(x) {
};
};
Now, I know that since foo has no default constructor with zero arguments this will not work. I would prefer not to add a meaningless constructor with zero arguments to foo for several reasons. Is there a nice way to get this working? Right now my compiler (gcc under Ubuntu) gives me the following error:
error::no matching function for call to foo::foo()
A quick attempt to try and work around this is by replacing the constructor of bar:
bar(int y, int z) : foo(y)
But this also does not work.
I have not been able to find a working solution for this yet online, and would be gratefull for any help.
_foo_ptr_1 = foo::foo(y)is not how you construct classes in C++!foo *_foo_ptr_1;in your real code? Where is this error message issued?baris afooitself, are you sure you need additional twofoos inside? Maybe one additionalfoowould be enough?barneed its own ID? in which case it should accept one as an argument. If it does not need an ID, why is it inheriting fromfoo?