Inheritance Problems

Started by
4 comments, last by evilsanta 17 years, 6 months ago
Alright, so im messing around with some stuff in C++, and I have a question reguarding vectors etc. etc.

#include <cstdlib>
#include <iostream>
#include <vector>


using namespace std;

class object{
    public:
        object(){
            //cout << "object created \n";
        }
        void update(){
            cout << "OBJECT!!!" << "\n";
        }
        void setCost(int newCost){}
};
vector<object> objectVector;
vector<object>::iterator objectIterator;

class fruit: public object{ //EXTENDS OBJECT
    float fruitCost;
    public:
        fruit(){ //Constructor
            //cout << "fruit created \n";
        }
        fruit(float newFruitCost){ //Overload Constructor, uses this if param met
            fruitCost = newFruitCost;
            cout << "fruit created OVERLOAD\n";
        }
        void setCost(float newFruitCost){
            fruitCost = newFruitCost;
        }
        float getCost(){
            return fruitCost;
        }
        void update(){
            cout << fruitCost << "\n";
        }
};
int main(){
    objectVector.push_back(fruit(0)); //create a null one that isnt counted
 
    float input;
    
    while (1){
        cout << ":";
        cin >> input;
        if (input == 0){ //ITERATE
            for (objectIterator = objectVector.begin()+1;
                objectIterator != objectVector.end();
                objectIterator++) {
                    objectIterator -> update();
            }
        }else{ //CREATE A NEW FRUIT
            objectVector.push_back(fruit(input));
            /* USE THIS FOR GRABBING THE RECENTLY MADE ONE
            objectIterator = objectVector.end()-1;
            objectIterator -> setCost(y);
            */
        }
    }
    system("PAUSE");
    return 0;
}




Now in the above code, if you enter the input 1, 2, and 3 then 0 you get 3 OBJECT!!! lines, now what I want is to hold all my objects in one vector, as if the following vector<object> objectVector; vector<object>::iterator objectIterator; is changed to vector<fruit> objectVector; vector<fruit>::iterator objectIterator; Then it works how I want it to, it outputs the cost, but declaring a vector etc. etc. for each class I might have in a game just seems redundant... is what I want even possible? Is there a better way to code the above? Thanks!
Advertisement
Ummm. I think that you have a "shadowing" problem. You need to make you functions in your parent class "virtual" so that they do not *shadow* fruits functions.
Thanks for the reply, but when I add virtual to my object member function update, it still does the same thing...

But I did some research on virtual more, am I correct in saying that its b/c my pointer isnt pointing at "fruit"? Ive been trying to implement pointers to the iterating, but so far I havent had much luck. :(
Ok Im actually relatively new to C++ so this definition may not be exactly correct - and someone PLEASE CORRECT ME IF IT ISN'T!!!.
This is what i think is happening:

You have a vector of objects. Each position in your vector is only allocated enough memory for a single "object" object(lol). But a "fruit" object is larger than an "object" object because it is a subclass. So when you try to add a "fruit" object into your vector, it is getting sliced back to the size of an "object" object. Thus all your objects in your vector are actually of type "object".

Solution:
Pointers are all the same size. So create a vector<object *> instead. Now you will have to go "push_back(new fruit(x))" when adding items to your vector. So now you are just adding a pointer to a new "fruit" object, which is the same size as a pointer to an "object" object.

update() still needs to be virtual

Hope that helps.
One other thing:

1)Because you have a vector of pointers, you will need to dereference your iterator and then do the indirection operator thing(i dont know what its called) to access your update() function on your object:

(*objectIterator)->update();



Excellent, that fixed it thanks much!

This topic is closed to new replies.

Advertisement