0

So I have a class Scene which contains a vector of elements of another class ´Object´. Now, in my main() I want to make changes to an Object, and this must be also applied to the instance of the object inside the Object vector in the class. My idea was to create a function that puts the object inside the vector, and returns a reference to it, so that from the main you can change it and make changes affect the vector. It works perfectly with just one object. The problem is that, if I use more than 1 object, it just stops working, and none of the objects changes its position, not even the first one that used to work before. What's the problem? Is there a better way to do this (I'm new to c++ hehe) Thanks! My code is this:

class Object{
private:
    int posX, posY;
    string name;

public:
    Object(){
        name = "Object" + id;
    }

    int getPositionX(){
        return posX;
    }

    int getPositionY(){
        return posY;
    }

    void setPosition(int positionX, int positionY){
        posX = positionX;
        posY = positionY;
    }

};

My scene class:

class Scene{
private:
    vector <Object> objectsInScene;

public:
    Scene(){
        cout << "Scene created";
    }

    vector <Object> getObjectsInScene(){
        return objectsInScene;
    }

    Object& insertObject(Object newObject){
        objectsInScene.push_back(newObject);
        return objectsInScene[objectsInScene.size()-1];
    }

};

and my main:

int main(){
    Scene mScene;
    Object myObj;
    Object secondObj;
    secondObj.setPosition(15,15);
    myObj.setPosition(7,7);
    Object& myObject = mScene.insertObject(myObj);
    Object& secondObject = mScene.insertObject(secondObj);

    //other things
    switch(cin.get()){
       case 'w':
          myObject.setPosition(3,3);///This is the one that doesn't work
          break;
    }
}
  • 2
    When you add elements to a vector, it might need to change its *capacity*, and that usually means the memory inside the vector object needs to be reallocated. This invalidates *all* existing pointers, references and iterators to elements in the vector. In short, it's not possible to do what you do. – Some programmer dude Jan 17 '18 at 12:03
  • I'm not a c++ expert, but your function "getObjectsInScene()" return copy of your vector, containing copies of your objects. I think you came to C++ from Java or C#, and you don't grasp the differences. You use vector of pointers (preferably smart pointers) and then returning copy of vector of pointers does what you think it will – Krzysztof Skowronek Jan 17 '18 at 12:06
  • @Someprogrammerdude uh. And there is no way to somehow keep the reference? maybe creating a vector which is way bigger than needed so the size doesnt need to be modified? Thanks – Romolo Caponera Jan 17 '18 at 12:06
  • @KrzysztofSkowronek yes, I came from java hehehe. So I should change the vector to ´vector´? I'm not very familiar with pointers as you may have noticed hehehe – Romolo Caponera Jan 17 '18 at 12:11
  • @RomoloCaponera Of course you can create a vector with enough big capacity. But then what happens if that capacity turns out to not be enough? Or what will you do with the wasted memory that is not used? I think you should at least try to reconsider the design you have, and the requirement that you get references. *Why* do you need to return references? – Some programmer dude Jan 17 '18 at 12:11
  • @Someprogrammerdude I don't really need a reference but to my inexpert eyes it seemed the best solution. What I would want is a way to modify the vector without having to use a `Scene` function. Is that possible? – Romolo Caponera Jan 17 '18 at 12:15
  • that's why I stick to C#. Shortcut would be to add function GetObject(position) to your scene, that returns single reference. And don't use Object*, use smart pointer instead. That's the closest thing you can get to Java reference: https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one – Krzysztof Skowronek Jan 18 '18 at 12:59

0 Answers0