1

This isn't an actual programming question, I just need advice on how to structure a part of my program.

The program is divided into a client and a server part. Both share certain code, including base classes. Here's a representation of the code I'm having trouble with:

Shared classes:

class BaseEntity
{
public:
    virtual void EmitSound(std::string snd) {}
    virtual bool IsPlayer() {return false;}
};

class BasePlayer
    : public BaseEntity
{
public:
    bool IsPlayer() {return true;}
}

Serverside classes:

class Entity
    : public BaseEntity
{
public:
    void EmitSound(std::string snd)
    {
        // Serverside implementation
    }
}

class Player
    : public Entity,BasePlayer
{
public:
    void Kick() {}
}

Clientside classes:

class CEntity
    : public BaseEntity
{
public:
    void EmitSound(std::string snd)
    {
        // Clientside implementation
    }
};

class CPlayer
    : public CEntity,BasePlayer
{
public:
    void SetFOV() {}
}

(The methods are just examples)

The class 'Player' should inherit all members from both 'Entity' and 'BasePlayer'. This doesn't work however, since both have the same parent class (BaseEntity).

I could of course remove the inheritance from 'BasePlayer' but then I'd end up with two 'IsPlayer' functions, each returning something different.

I could also move all members from 'BasePlayer' into 'Player' and 'CPlayer' respectively, but that'd lead to redundancy I'd like to avoid and I'd no longer be able to use a single pointer for objects of either class while keeping access to all shared methods.

I don't like either of these solutions, but I can't think of anything else. Is there an optimal solution to this problem?

1
  • What is the purpose of an "entity", and why does Player have to inherit from it, as well as BasePlayer needing to inherit from BaseEnttity? To me, this seems like a mixup of different classes that isn't "the way it's supposed to be". Commented Jun 1, 2013 at 15:44

1 Answer 1

1

the simplest solution i see that i think would solve your specific problem would be to have class BasePlayer not inherit from class BaseEntity.

class Player will end up with the BaseEntity class characteristics because it inherits from class Entity which inherits from class BaseEntity, and class CPlayer will end up with BaseEntity class characteristics because it inherits from class CEntity which inherits from class BaseEntity.

Sign up to request clarification or add additional context in comments.

2 Comments

That's the dilemma. BasePlayer NEEDS to inherit from BaseEntity, because BaseEntity defines things like its position and orientation (And a lot more). I probably should've made that more clear in the example.
@Silverlan, to refer to position & orientation, there's still no need to have BasePlayer inherit from BaseEntity. in the case where you already are referring to Player/CPlayer objects, you'll pick up the ability to refer to these position & orientation for free since you'd be inheriting from BaseEntity via Entity & CEntity, and in the case where you need to refer to them independent of the type of BasePlayer, then refer to them directly as BaseEntity objects … which will be more clear & appropriate reference to the class defining position & orientation, anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.