Search code examples
c++overloading

C++ - Can you overload a function with different parameters but define it only once?


I'm a beginner in c++ and I'm trying to make a simple command line game. I have three classes:

class DynamicEntity : public Entity { // Entity class has only int x, y variables and getters/setters
    protected:
        enum direction {RIGHT = 77, LEFT = 75, UP = 72, DOWN = 80};
    public:
        posWrap getNextPos(); // Returns the xy coordinates of the entity's next position in a struct called posWrap
};

class Player : public DynamicEntity {
    private:
        int wishdir;
    public:
        int ammoCount = 0;
        posWrap getNextPos(); // Same concept as above, but takes input
};

class CollisionHandler {
    public:
        static char checkCollisions(DynamicEntity& dEntity); // Checks the next position of the entity for collisions and returns what it would collide with
        static char checkCollisions(Player& player); 

};

You see, I have overloaded the checkCollisions method. And defined the DynamicEntity version as such:

char CollisionHandler::checkCollisions(DynamicEntity& dEntity) {
    int x = dEntity.getNextPos().x;
    int y = dEntity.getNextPos().y;

    return readChar(x, y);
}

The implementation of the getNextPos functions are different for Player and DynamicEntity, as the first one takes an input from the player while the latter gets its orders from somewhere else in the program. I want to know if it is possible to pass in a Player class to checkCollisions and inside the method, use the Player version of getNextPos, without explicitly writing another definition of checkCollisions. I thought it would be possible since Player is a child of DynamicEntity. Or am I asking too much of the compiler?

Let me know if you have any other suggestions, criticism or a better way to do this stuff. I am trying to learn more in-depth about these topics. Thanks in advance!


Solution

  • I want to know if it is possible to pass in a Player class to checkCollisions and inside the method, use the Player version of getNextPos, without explicitly writing another definition of checkCollisions.

    You seem to misunderstand how overloading and virtual dispatch works. This function

    static char checkCollisions(DynamicEntity& dEntity);
    

    can be called with a Player. You do not need the other overload. Though to make it work as expected you should declare getNextPos() as virtual, such that when checkCollisions(DynamicEntity&) is called with a Player& it will call Player::getNextPos():

    class DynamicEntity {
        public:
            virtual posWrap getNextPos();
          //^^
            virtual ~DynamicEntity(){} // polymorphic types need a virtual destructor
    };
    
    class Player : public DynamicEntity {
            posWrap getNextPos() override; // <- add override to help with typos               
    };
    
    class CollisionHandler {
        public:
            static char checkCollisions(DynamicEntity& dEntity);    
    };
    

    PS:

    Can you overload a function with different parameters but define it only once?

    No. You cannot call a function that has no definition. If you actually try it you would get a linker error.