Hi there
I have this problem where I made a class SettingCategory which takes a string and a vector as parameter and on the other hand i have a new class ControlManager which owns a SettingCategory:
but the compiler tells me SettingCategory has no default constructor. I guess when the ControlManager is created it already tries to create the SettingCategory and my way of creating controls_ in the last line is then illegal? I thought of using an initializer list but I guess I can't create my vector there? Also I could make controls_ a pointer but is there another way?
greetings
I guess when the ControlManager is created it already tries to create the SettingCategory
Yes, your controls_ object is constructed before the body of the ControlManager ctor even begins. Which means it will fail to construct unless you explicitly call a constructor for it in the initialization list, or unless it has a default constructor.
my way of creating controls_ in the last line is then illegal?
No, that line is perfectly legal. You're constructing a new SettingCategory and assigning it to controls_. That will work fine. The problem is that is an assignment it is not a construction. The construction has already happened.
Also I could make controls_ a pointer but is there another way?
That is what I would do.
1 2 3 4 5 6 7 8 9 10 11
class ControlManager
{
public:
ControlManager();
private:
std::unique_ptr<SettingCategory> controls_;
};
//...
controls_ = std::unique_ptr<SettingCategory>( new SettingCategory("Controls", controlsSe) );
Alternatively, you could give SettingCategory a default constructor.
EDIT:
Cubbi's solution is superior to mine. I forgot about C++11's ability to initialize vectors in the initialization list.
I like the look of cubbi's solution too, i'm a bit confused about one thing though:
why can you construct the whole vector without using the keyword vector in the parameter?
is it because the SettingCategory takes a vector as parameter? And inside the vector you create the Setting object only with its parameter values? :D I've never seen this way of writing that before.