Jump to content
  • Advertisement
Sign in to follow this  
evilsanta

Inheritance Problems

This topic is 4293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :(

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!