Defining all possibilities of classes to be passed, how not to do it

Ummmm

I have a class called winCtrl.
I have 3 more classes based on winCtrl called winCtrlTextBox, winCtrlLabel and winCtrlButton.

The code below works but I think it can be improved in a particular way. I am trying to work out how to improve it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void wfCB::create(winCtrlCltn * collection, int type) {
	winCtrlTextBox ctrltest1;
	winCtrlLabel ctrltest2;
	winCtrlButton ctrltest3;
	switch(type) {
		case 1:
			collection->addCtrl(&ctrltest1);
			break;
		case 2:
			collection->addCtrl(&ctrltest2);
			break;
		case 3:
			collection->addCtrl(&ctrltest3);
			break;
	};
}


It currently requires me to create an instance of each class when I only want to create the class I am passing.

The code below is what I would like my code to look like but I don't know what to put on each line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void wfCB::create(winCtrlCltn * collection, int type) {
	winCtrl ctrltest;
	switch(type) {
		case 1:
			ctrltest = ? //winCtrlTextBox
			break;
		case 2:
			ctrltest = ? //winCtrlLabel
			break;
		case 3:
			ctrltest = ? //winCtrlButton
			break;
	};
	collection->addCtrl(&ctrltest);
}


In VB, I would write something like...

 
ctrltest = new winCtrlTextBox()


Any suggestions as to how I can achieve my aim in C++?

The code was only created to demonstrate my question. I am aware it contains issues... but please focus on the question I am asking :)

Thanks
Ummmm

I have a class called winCtrl.
I have 3 more classes based on that winCtrl called winCtrlTextBox, winCtrlLabel and winCtrlButton.

The code below works but can be improved. I am trying to work out how to improve it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void wfCB::create(winCtrlCltn * collection, int type) {
	winCtrlTextBox ctrltest1;
	winCtrlLabel ctrltest2;
	winCtrlButton ctrltest3;
	switch(type) {
		case 1:
			collection->addCtrl(&ctrltest1);
			break;
		case 2:
			collection->addCtrl(&ctrltest2);
			break;
		case 3:
			collection->addCtrl(&ctrltest3);
			break;
	};
}


It currently requires me to create an instance of each class when I only want to create the class I am passing.

The code below is what I would like my code to look like but I don't know what to put on each line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void wfCB::create(winCtrlCltn * collection, int type) {
	winCtrl ctrltest;
	switch(type) {
		case 1:
			ctrltest = ? //winCtrlTextBox
			break;
		case 2:
			ctrltest = ? //winCtrlLabel
			break;
		case 3:
			ctrltest = ? //winCtrlButton
			break;
	};
	collection->addCtrl(&ctrltest);
}


In VB, I would write something like...

 
ctrltest = new winCtrlTextBox()


Any suggestions as to how I can achieve my aim in C++?

The code was only created to demonstrate my question. I am aware it contains issues... but please focus on the question I am asking :)

Thanks
Make three functions?

I guess I don't understand your API, is this a Window's Form? Using Win32 API, when you create the window you define a parent window. You don't need any "Master Controller" class.
Please don't worry about WHAT the code does. I am looking for syntax info....

How can I do stuff like the described without creating lots of classes when I only want one?
collection->addCtrl(new winCtrlTextBox());

Maybe??

Topic archived. No new replies allowed.