dimanche 13 septembre 2020

How to properly implement an addObject() function in C++?

I am new to this website.
I am sorry if this has been asked but I just can't find any questions / solutions that answer my question since I think this is a pretty specific question.
So I have been learning how to create a game engine with SFML in C++ and I have a question regarding how to implement an addObject() function that, upon being called, creates an object that gets added to a vector of objects in a class in an optimized manner.
Here is my attempt:
Object.h

class Object{
private:
    string m_Type;
    string m_Action;
    int m_PositionX;
    int m_PositionY;

public:
    Object(string type, string action, int positionX, int positionY){
        m_Type = type;
        m_Action = action;
        m_PositionX = positionX;
        m_PositionY = positionY;
    }

};

ListOfObjects.h (where the addObject() function is)

class ListOfObjects{
private:
    vector<unique_ptr<Object>> m_Objects;

public:
    void addObject(string type, string action, int positionX, int positionY){
        m_Objects.push_back(move(make_unique<Object>(type, action, positionX, positionY)));
    }
};

Now this works fine but I figured there might be better ways to implement this.
The reason why I have this thought is that when I tried to change the constructor of the Object class, say I want to add one more parameter (e.g. Object(std::string m_Type, std::string m_Action, int PositionX, int PositionY, int Scale);), now I would need to also update the parameters of the addObject(...) function as well as well as the following line of code where I called the make_unique function. That is a lot of work (or not, really I don't know, I'm still a beginner lol). So I figured can I make the addObject(...) take a temporary object instead of a list of parameters to match the constructor of the Object class itself?
ListOfObjects.h

class ListOfObjects{
private:
    std::vector<std::unique_ptr<Object>> m_Objects;

public:
    void addObject(Button button){
        m_Objects.push_back(move(make_unique<Object>(button)));
    }
};

Now, whenever I need to change the constructor of the Object class, I don't need to update the addObject(...) function. But now, I need to add Object like this:

int main(){
    addObject(Button("name", "action", 100, 50));
    //and then a lot of random errors associated with memory and pointer stuff :(
    return 0;
}

I actually tried to implement this but there is just a lot of errors and my small brain just can't comprehend what went wrong. I don't know if passing a temporary object into a function is an expensive operation. Should I pass in a reference of the temporary object like this? addObject(Button& button)

So the main question is:
Is there a better way to implement this function to add an object such that adding more parameters to the constructor of Object does not require me to update the addObject(...) function?

Any help would be much appreciated! (And sorry for such a long question :<)

Aucun commentaire:

Enregistrer un commentaire