[英]C++ Vector of Subclasses, using it to overload the istream/ostream
I'm looking to hold a Vector of Objects, of which will be Subclasses. 我正在寻找持有对象的向量,其中将是子类。
I thought I would be able to do it by declaring a Vector of Pointers to the Baseclass (Such as vector<BaseClass*> db
), and then declare it as a Subclass by doing something like db.pushback(new subclass)
(My example in the link below is a touch different, but along the same lines); 我以为可以通过向基类声明一个指针向量(例如vector<BaseClass*> db
),然后通过执行类似db.pushback(new subclass)
声明它为子类的方式来做到这一点(我的示例在下面的链接中有所不同,但沿同一行);
Is it possible to store Multiple subclasses in this sense Or will I need to define a new Vector for each SubClass? 是否可以在这种意义上存储多个子类?还是需要为每个子类定义一个新的Vector? In the example given, there is only 1, but realistically in my program there is four. 在给出的示例中,只有1,但是实际上在我的程序中有4。
If so, in my overloaded >> in SubClass1, would dynamic casting the type to a BaseClass work to call the friended overloaded >> in the BaseClass? 如果是这样,在SubClass1中的重载>>中,将类型动态转换为BaseClass是否可以在BaseClass中调用友好的重载>>?
http://ideone.com/QM5sRY http://ideone.com/QM5sRY
Edit: 编辑:
Sorry, I wasn't entirely clear in my second half of the question. 抱歉,问题的后半部分我还不清楚。 I should have expanded. 我应该扩大。
I have a program which needs to take an input, and distribute it throughout the respective Classes and Subclasses. 我有一个需要输入的程序,并将其分配到各个类和子类中。 It should take the input as Cin >> class;
输入内容应为Cin >> class;
, in which case I have overloaded the >> operator. ,在这种情况下,我要重载>>运算符。
However, when I define the data as the Subclass (lines 34 to 39, and line 44), it appears to call it as a BaseClass, rather than a Subclass. 但是,当我将数据定义为Subclass时(第34至39行以及第44行),它似乎将其称为BaseClass,而不是Subclass。 It then calls the friend function defined in the Baseclass at line 10, rather than in than line 21. 然后,它在第10行而不是第21行调用在基类中定义的friend函数。
I'm not completely sure where I am going wrong. 我不确定自己要去哪里。
Ideally the output should be 理想情况下,输出应为
Printing:Data
X = 1
Y = 2
You should have a virtual fromSerial function that reads in the necessary data for each class. 您应该有一个虚拟的fromSerial函数,该函数读取每个类的必要数据。 Here is an example http://ideone.com/WGwj8l . 这是一个示例http://ideone.com/WGwj8l 。 Also notice the user of virtual keyword. 还要注意虚拟关键字的用户。 You need that for polymorphism. 您需要它来实现多态。 And note the virtual destructor as well. 还要注意虚拟析构函数。
#include <iostream>
#include <vector>
using namespace std;
class BaseClass{
public:
int x;
public:
BaseClass(){x = 0;}
virtual istream& fromSerial(istream& stream){ return stream >> x; }
virtual void print(){
cout << "BaseClass::x = " << x << endl;
}
virtual ~BaseClass(){}
};
class SubClass1: public BaseClass{
public:
int y;
public:
SubClass1(){y = 0;}
virtual istream& fromSerial(istream& stream){
BaseClass::fromSerial(stream); //read baseclass first
return stream >> y;
}
virtual void print(){
BaseClass::print();
cout << "SubClass1::y = " << y << endl;
}
};
BaseClass* createNewClass(BaseClass * temp)
{
cout << "Input 2 values: ";
temp->fromSerial(cin);
return temp;
}
int main()
{
vector<BaseClass*> db;
db.push_back(createNewClass(new SubClass1));
cout << "\nPrinting Data: " << endl;
db[0]->print();
}
Input: 1 2
输入: 1 2
Output: 输出:
Input 2 values:
Printing Data:
BaseClass::x = 1
SubClass1::y = 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.