Generic Programming: How to make things independent?

I've been thinking a lot about generic programming lately and how it can benefit me. Though I have came across something I'm not sure of. Either I am doing it wrong which is most likely, or it's just the way to make things generic.

Let me give you some example code:

1
2
3
4
5
6
7
8
9
10
11
12
template <class Stats, class Skills>
class Player
{
private:
	std::vector<Stats> PlayerStats;		// Player stats - Health, Mana etc
	std::vector<Skills> PlayerSkills;	// Player skills - Slash, Fireball etc
	std::string PlayerName;
public:
	Player(std::string n, std::vector<Stats> stats, std::vector<Skills> skills)
	: PlayerName(n), PlayerStats(stats), PlayerSkills(skills) {}
	~Player() {}
};



Now, how does one "use" or "display" it's stats or skills?

If this was for a game engine, how do I know what variables or methods people would use in their modeled classes?

My progress so far is to use a common method, for instance:

PlayerSkills.Use()

So no matter what class a person makes to model it's statistics or skills would have to include a method called "Use" (But again, does this return a float? A string? Another class?)


I'm struggling to identify if this method is too closesly couple and not generic enough.

Something I have though about but haven't tried, is making a base class that already has the function "Use" and people derive from this and ( override? ) the Use function.


Any help would be appreciated.
I'm pretty sure polymorphism is what you should be using here, not templates, as at the moment, PlayerStats and PlayerSkills could be anything, which is just wrong. Instead, anything that they could validly be should inherit from an abstract base class that contains the functionality that PlayerStats or PlayerSkills would definitely need.
Last edited on
as at the moment, PlayerStats and PlayerSkills could be anything


That is the whole point; I want a "Generic Player" class, where people can plug in their own polymorphic classes which will be modeled by the regulation of their game.


I don't want to intrude on their design and come up with the classes for them. I want a game engine that can handle 2 types of generic objects, a vector of player statistics and player skills.

Instead, anything that they could validly be should inherit from an abstract base class that contains the functionality that PlayerStats or PlayerSkills would definitely need.


I mentioned this in the later part of my post.
I know you did, I was agreeing with you. I was saying that as some classes would not be appropriate as classes do not all contain the same methods (as you said) so you would have to specialise your templating not only to include all primitive classes, but all classes that the user could create, which is impossible. I was suggesting maybe providing an abstract base class, from which the user could inherit their own polymorphic classes and override virtual members such as Use().
I was suggesting maybe providing an abstract base class, from which the user could inherit their own polymorphic classes and override virtual members such as Use().


This is reminding me of the Unity style iMyScriptableObject and the like.

My ideal scenario would be allowing people to come up with their own objects, and a generic engine that would piece all the data together based on what the object is supposed to be, such as a stat or a skill (This will be known at compile time though the template).

I was thinking I could use Functors rather than abstraction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename T>
class Stat
{
private:
	T Value;
public:
	Stat() {}
	~Stat() {}
	void operator()(T v)
	{
		Value = v;
	}
};

// Somewhere down the line

PlayerStats[0](15); Element 0 is "Health", assign it 15
Topic archived. No new replies allowed.