Is it possible declare a variable with different types in c++?
I'm trying to declare a variable like this:
switch(foo){
case 1:
MyClass myObject();
break;
case 2:
MyClass2 myObject();
break;
case 3:
MyClass3 myObject();
break;
}
but at moment to compile this, compiler returns these errors:开发者_运维技巧
- crosses initialization of 'MyClass myObject'
- conflicting declaration 'MyClass2 myObject'
- 'myObject' has a previous declaration as 'MyClass myObject'
any idea to solve this?
The body of a switch
statement is a single scope. If you want to declare a variable local to a single case
you will have to create a block in the case
using curly braces:
switch (foo) {
case 1: {
MyClass myObject();
break;
}
case 2: {
MyClass2 myObject();
break;
}
case 3: {
MyClass3 myObject();
break;
}
}
If you find you do this often you could consider creating functions for the code in each case
to get code that is easier to understand and maintain:
switch (foo) {
case 1:
HandleCase1();
break;
case 2:
HandleCase2();
break;
case 3:
HandleCase3();
break;
}
No, you cannot do something like that. Besides the naming conflict inside the switch, you also have the problem that the variable goes out of scope when you leave the switch.
On top of that, MyClass myObject();
declares a function returning a MyClass
. You would declare a variable as just MyClass myObject;
.
If you need three different object, perhaps you should write three different functions. Or perhaps a template function that can be used with different types.
What are you trying to achieve ? If you try to declare a variable and use it later, then:
Use inheritance and Factory for this.
ParentObject * FactoryCreator::createObject(cstring type){
ParentObject * theObject = null;
switch(type){
case "type1":
theObject = new ObjectType1();
break;
case "type2":
theObject = new ObjectType2();
break;
...
}
return theObject;
}
And from your code.
ParentObject object = FactoryCreator::createObject("type you want");
And you have to create a structure where ParentObject is abstract and all the type of objects are inherit from it.
You can check what Bo Persson told you about declaring variables here. I think that what you want to do is use polymorphism. That is make MyClass1
, MyClass2
and MyClass3
be derived objects of a BaseClass
and then you can do something like:
BaseClass *myObject;
switch (foo) {
case 1: {
myObject = new MyClass1;
break;
}
case 2: {
myObject = new MyClass2;
break;
}
case 3: {
myObject = new MyClass3;
break;
}
}
now you can let myObject behave like the object you declared and it can be used not only during the scope of that switch statement.
You might try boost::variant and the use a visitor:
typedef boost::variant<MyClass1, MyClass2, MyClass3> MyVariantClass;
boost::shared_ptr<MyVariantClass> myObject;
switch (foo) {
case 1: {
MyClass1 object();
myObject = boost::shared_ptr<MyVariantClass>(new MyVariantClass(object))
break;
}
case 2: {
MyClass2 object();
myObject = boost::shared_ptr<MyVariantClass>(new MyVariantClass(object))
break;
}
case 3: {
MyClass3 object();
myObject = boost::shared_ptr<MyVariantClass>(new MyVariantClass(object))
break;
}
}
And then create a visitor as boost guys says in their tutorial. The only issue here is that you really create two objects. The object itself in the declaration (MyClass3 object()) and then the copy the variant is doing inside.
精彩评论