Instantiation of an object with vector in constructor

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef CONTROLMANAGER
#define CONTROLMANAGER

#include "SettingCategory.h"

class ControlManager
{
public:
	ControlManager();
private:
	SettingCategory controls_;
};

#endif 


So the issue at this point is writing a constructor for this ControlManager

I tried writing it in the following way:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "ControlManager.h"

ControlManager::ControlManager()
{
	vector<Setting> controlsSe;
	controlsSe.push_back(Setting("Up", "Keyboard_Up", SettingTypes::Key));
	controlsSe.push_back(Setting("Down", "Keyboard_Down", SettingTypes::Key));
	controlsSe.push_back(Setting("Left", "Keyboard_Left", SettingTypes::Key));
	controlsSe.push_back(Setting("Right", "Keyboard_Right", SettingTypes::Key));
	controlsSe.push_back(Setting("Enter", "Keyboard_Return", SettingTypes::Key));
	controlsSe.push_back(Setting("Back", "Keyboard_Escape", SettingTypes::Key));
	controls_=SettingCategory("Controls", controlsSe);
}


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
The member initializer list is the right way to do this. With a sufficiently modern compiler, you can just write

1
2
3
4
5
6
7
8
9
10
11
12
ControlManager::ControlManager()
:controls_("Controls",
 {
    {"Up",    "Keyboard_Up",     SettingTypes::Key},
    {"Down",  "Keyboard_Down",   SettingTypes::Key},
    {"Left",  "Keyboard_Left",   SettingTypes::Key},
    {"Right", "Keyboard_Right",  SettingTypes::Key},
    {"Enter", "Keyboard_Return", SettingTypes::Key},
    {"Back",  "Keyboard_Escape", SettingTypes::Key}
 })
{
}

demo: http://ideone.com/dihxq7
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.
Last edited on
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.
Topic archived. No new replies allowed.