[英]c++ Using subclasses
I have this variable; 我有这个变量; Furniture **furnitures;
家具**家具; Which is an abstract baseclass to 2 subclasses, Bookcase and Couch.
这是2个子类,Bookcase和Couch的抽象基类。 I add these randomly;
我随机添加这些;
furnitures[n++] = new Bookcase ();
furnitures[n++] = new Couch();
.
.
For the sake of explaination. 为了解释。 Lets set some minor variables.
让我们设置一些小变量。 Furniture private: name, prize Bookcase private: size Couch private: seats
私人家具:名称,奖项私人书柜:大小沙发私人:座位
How would I go about if I wanted to print out information such as; 如果我想打印出诸如此类的信息,我将如何进行; name and seats?
名字和座位?
There are various of problems in this issue. 这个问题有很多问题。 1, distinguish which subclass is which when I use Furniture[i].
1,区分哪个子类是什么时候我使用Furniture [i]。 2, I dont want to blend too much unneccessary functions between the two subclasses that arent needed.
2,我不想在不需要的两个子类之间混合太多不必要的函数。
class Furniture
{
virtual void output() = 0;
};
class Couch : public Furniture
{
void output() override;
};
class Bookshelf : public Furniture
{
void output() override;
};
You could define the function in Furniture to save from duplicate code in subclasses like this: 您可以在Furniture中定义函数以从子类中的重复代码中保存,如下所示:
void Furniture::output()
{
// We assume here the output is to cout, but you could also pass the necessary
// stream in as argument to output() for example.
cout << name << price;
}
void Couch::output()
{
Furniture::output();
cout << seats;
}
void Bookshelf::output()
{
Furniture::output();
cout << size;
}
You should never use arrays polymorhphically . 你永远不应该以多态方式使用数组 。 Read the first item (I think it's the first) in Scott Meyers' More Effective C++ book to find out why!
阅读Scott Meyers的“ 更有效的C ++”一书中的第一项(我认为这是第一项),找出原因!
In fact, you should almost never use raw arrays in C++ anyway. 事实上,你几乎不应该在C ++中使用原始数组。 A correct solution is to use a
std::vector<Furniture*>
. 一个正确的解决方案是使用
std::vector<Furniture*>
。
How would I go about if I wanted to print out information such as;
如果我想打印出诸如此类的信息,我将如何进行; name and seats?
名字和座位?
There are various of problems in this issue.
这个问题有很多问题。 1, distinguish which subclass is which when I use Furniture[i].
1,区分哪个子类是什么时候我使用Furniture [i]。 2, I dont want to blend too much unneccessary functions between the two subclasses that arent needed..
2,我不想在两个不需要的子类之间混合太多不必要的函数。
You are facing this problem because you are abusing object-oriented programming. 您正面临这个问题,因为您正在滥用面向对象的编程。 It's simple: object-oriented programming makes sense when different types implement an abstract common operation and the concrete type is chosen at run-time.
这很简单:当不同类型实现抽象公共操作并且在运行时选择具体类型时,面向对象编程是有意义的。 In your case, there is no common operation.
在您的情况下,没有共同的操作。 Printing (or receiving) the number seats is for one type, printing (or receiving) a size is for the other type.
打印(或接收)数字座位是一种类型,打印(或接收)的尺寸是另一种类型。
That's not to say that it's bad or wrong, but it's simply not object-oriented. 这并不是说这是坏事还是错误,但它根本不是面向对象的。
Now C++ would not be C++ if it didn't offer you a dangerous tool to get out of every dead end you've coded yourself into. 现在C ++不会是C ++,如果它没有为你提供一个危险的工具来摆脱你自己编码的每一个死胡同。 In this case, you can use Run-Time Type Identifcation (RTTI) to find out the concrete type of an object.
在这种情况下,您可以使用运行时类型标识(RTTI)来查找对象的具体类型。 Google for
typeid
and dynamic_cast
and you'll quickly find the solution. 谷歌的
typeid
和dynamic_cast
,你会很快找到解决方案。 But remember, using RTTI for this problem is a workaround . 但请记住,使用RTTI解决此问题是一种解决方法 。 Review your class design, and change it if necessary.
检查您的课程设计,并在必要时进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.