简体   繁体   中英

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);

  • Is it possible to store Multiple subclasses in this sense Or will I need to define a new Vector for each SubClass? In the example given, there is only 1, but realistically in my program there is four.

  • If so, in my overloaded >> in SubClass1, would dynamic casting the type to a BaseClass work to call the friended overloaded >> in the BaseClass?

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; , 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. It then calls the friend function defined in the Baseclass at line 10, rather than in than line 21.

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. Here is an example 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

Output:

Input 2 values: 
Printing Data: 
BaseClass::x = 1
SubClass1::y = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM