简体   繁体   中英

Vector of pointers to inherited class objects c++

I have three clases:

A{ abstract class }

B inherits A

C inherits A

I need to create a class D that contains a vector<A*> vect in which objects of both class B and C are stored. My idea was to create classA pointers and assign them the address of classB and C objects. From what I gathered this is called upcasting?

However, only the classA attributes are stored in the vector, the inherited attributes aren't.

Here's the class that contains the vector:

class D{
    vector<A*> vect;
public:
    MagazinVirtual& operator+=(A* obj){
        vect.push_back(obj);
        return *this;
    }
    friend ostream& operator<<(ostream& out, D &obj){
        vector<A*>::iterator it;
        for (it = obj.vect.begin(); it != obj.vect.begin(); ++it)
            out << **it << endl;
        return out;
    }
};

Class A variables:

    class A{
        static int cod;
        char* denumire;
        int garantie;
        float pret;
        int stoc;
};

Class B:

class B:public A{
    char* os;
    bool tipEcran;
    float diagonala;
};

Class C:

class C:public A{
    int tipAccesoriu;

Here's how I add objects to the vector: (Don't shoot me for the horrible names and chained constructor calls, it's for OOP class and for some reason they want the exact same calls in main();

int main(){
    B t1, t2("nokia 231", 2, 27, 10, "and", 1, 4);
    //t1 = t2;
    C a1("Husa EH-17", 1, 2, 100, 0), a2("Card SD64", 1, 10, 100, 2), a3 = a2;

    D dvect;
    A *ptr1 = &t1;
    A *ptr2 = &a1;
    a += ptr1;
    a += ptr2;
    cout << a;
return 0;
}

When using the overloaded ostream operator for the A class, despite the fact that there should be 2 elements in the vector(1B 1C), it only prints out the classA attributes of the FIRST pushed object, which is a classB object. In this case, it prints out:

nokia 231 / 2 / 27 / 10

Here's the ostream operators for class B:

friend ostream& operator<<(ostream &out, B&obj){
    out << (A&)obj << obj.os << endl <<obj.tipEcran
        <<endl<<obj.diagonala<<endl;
    return out;
}

And for class C:

friend ostream& operator<<(ostream &out, C &obj){
    out << (A&)obj << obj.tipAccesoriu << endl;
    return out;
}

Sorry for the long post and if it seems like a dumb question, but pointers genuinely make my head spin. Thank you for your time!

Run time polymorphism only occurs through virtual functions of the base class. Your operator<< methods are neither virtual nor members of the base class.

The usual solution is to define ostream& operator<<(ostream &out, A&obj) only for A and not for B or C but inside that function don't implement it directly. Instead have it pass the ostream& as a parameter to a virtual function of the class. Such as (inside A):

friend ostream& operator<<(ostream &out, A&obj){
    return A.serialize( out );
}
virtual ostream& serialize(ostream &out) { ... };

Then later declare and define:

ostream& B::serialize(ostream &out){
    A::serialize(out) << os << endl <<tipEcran
        <<endl<<diagonala<<endl;
    return out;
}

+It prints only one line because the condition is bad, it stops before! doing the first iteration.

for (it = obj.vect.begin(); it != obj.vect.begin(); ++it)
                                           ^^^^^

Usually, one puts there

obj.vect.end()

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