[英]Assigning different classes to a particular object depending on user input in C++
I have considered an object 'genObj'. 我考虑了一个对象“ genObj”。 This object can be of the class phones, laptops, shoes, or books.
该对象可以属于手机,笔记本电脑,鞋子或书籍等类别。 I wish to take user input varying from 1-4, and accordingly assign genObj to a praticular class.
我希望接受1-4之间的用户输入,并相应地将genObj分配给心室类。
I wish to do this because I have to add these objects to data files. 我希望这样做,因为我必须将这些对象添加到数据文件中。 I am maintaining 4 separate data files for the different classes, and a value to fileName is also assigned accordingly.
我为不同的类维护4个单独的数据文件,并且还为fileName分配了一个值。
switch(mode)
{
case 1:
phones genObj;
strcpy(fileName,"phonesDatabase.dat");
break;
case 2:
computers genObj;
strcpy(fileName,"computersDatabase.dat");
break;
case 3:
shoes genObj;
strcpy(fileName,"shoesDatabase.dat)";
break;
case 4:
books genObj;
strcpy(fileName,"booksDatabase.dat");
break;
}
Doing this, hovewer, gives the following error: 这样做会产生以下错误:
"Multiple declaration for genObj in function add(int)"
The values are assigned to fileName without problem as I have declared 值被分配给fileName就像我声明的一样
char fileName[20];
globally, and it gets initialized later. 全局,稍后将对其进行初始化。 However, since the object genObj can be of different classes, it cannot be initialized like this.
但是,由于对象genObj可以属于不同的类,因此无法像这样初始化。
If I am not able to assign genObj different classes according to user input, I will have to separate the code into 4 parts, each for one class. 如果我无法根据用户输入为genObj指定不同的类,则必须将代码分为4部分,每个部分用于一个类。 and that would be too cumbersome, and inelegant.
那太麻烦了,太笨拙了。
Thanks. 谢谢。
You cannot decide the type of an object dynamically at run-time. 您无法在运行时动态决定对象的类型。
Seems like you should use polymorphism in this situation: 似乎您应该在这种情况下使用多态 :
First define an abstract class, say object
, that defines the behavior that you use to access your genObj
. 首先定义一个抽象类,例如
object
,该类定义用于访问genObj
的行为。 That is, everything phones
, computers
, shoes
, and books
have in commons. 就是说,
phones
, computers
, shoes
和books
都具有共同点。
Then make those four sub-classes of object
and then something like this: 然后使
object
这四个子类,然后是这样的:
std::unique_ptr<object> genObj;
switch(mode)
{
case 1:
genObj = std::make_unique<phones>();
strcpy(fileName, "phonesDatabase.dat");
break;
case 2:
genObj = std::make_unique<computers>();
strcpy(fileName, "computersDatabase.dat");
break;
case 3:
genObj = std::make_unique<shoes>();
strcpy(fileName, "shoesDatabase.dat)";
break;
case 4:
genObj = std::make_unique<books>();
strcpy(fileName, "booksDatabase.dat");
break;
}
(Note that make_unique
is a feature only in the most recent C++ standard, so if you're writing your code for an old standard then you will have to use new
instead, eg genObj.reset(new books)
.) (请注意,
make_unique
仅在最新的C ++标准中是一项功能,因此,如果您为旧标准编写代码,则必须使用new
,例如genObj.reset(new books)
。)
Also as a side note, make mode
of an enum type, so that you don't have to handle magic numbers and can replace 1
, 2
, 3
, and 4
with some descriptive names such as mode::phones
, mode::computers
, mode::shoes
, and mode::books
respectively. 另外,作为一个侧面说明,使
mode
枚举类型的,这样你就不必处理幻数,可替代1
, 2
, 3
,和4
用一些描述性的名字,如mode::phones
, mode::computers
, mode::shoes
和mode::books
。
switch
construction won't handle dynamic allocation of objects for you. switch
构造不会为您处理对象的动态分配。 It'll be much better to employ a fabric here with common interface or abstract class (depending on your needs), say class IFace
, and then have a local variable IFace *genObj
and then make it equal to new some_derived_from_IFace
. 最好在这里使用具有通用接口或抽象类(取决于您的需要)的结构,例如
IFace
类,然后使用局部变量IFace *genObj
,然后使其等于new some_derived_from_IFace
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.