Using the example below... how can I achieve a structure which allows for this functionality? I want mixin style structs so i can add functionality in this way... but i would like to have generic functions that can be called on any struct (based on Beatle), like getExperienceInSingingAndGuitar()
struct Beatle {
int Age;
};
template<typename s>
struct ExperienceInSinging : public s {
int SingingYears;
ExperienceInSinging() {SingingYears = 10;};
};
template<typename s>
struct ExperienceInDrumming : public s {
int DrummingYears;
ExperienceInDrumming() {DrummingYears = 15;};
};
template<typename s>
struct ExperienceInGuitar : public s {
int GuitarYears;
ExperienceInGuitar() {GuitarYears = 20;};
};
template<typename s>
struct ExperienceInPiano : public s {
int PianoYears;
ExperienceInPiano() {PianoYears = 25;};
};
typedef ExperienceInSinging<ExperienceInGuitar<Beatle>> Paul;
typedef ExperienceInSinging<ExperienceInGuitar<ExperienceInPiano<Beatle>>> John;
typedef ExperienceInSinging<ExperienceInPiano<Beatle>> George;
typedef ExperienceInDrumming<Beatle> Ringo;
Beatle * member;
int getExperienceInSingingAndPiano() {
//clearly errors
return member->ExperienceInSinging() + member->ExperienceInPiano();
};
int getExperienceInSingingAndGuitar() {
//clearly errors
return member->ExperienceInSinging() + member->ExperienceInGuitar();
};
enum who {P = 1, J = 2, G = 3, R = 4};
int create(who x) {
switch(x) {
case P:
member = new Paul;
break;
case J:
member = new John;
break;
case G:
member = new George;
break;
case R:
member = new Ringo;
break;
};
};
int main() {
create(G);
int i = getExperienceInSingingAndPiano();
create(P);
int j = getExperienceInSingingAndGuitar();
return 0;
};
The point is that main() knows nothing of what struct is being used... but i can still have functions to access any functionality of any struct that is created by create().... ?
edit:
this could lead to code being called on variables that dont exist, but i want to ignore that for now :)
Why not post your actual project's data (if it isn't already) and what you want it to do. You might think this is similar but maybe it isn't.
Also there are some things to consider, why put the number of years (age and experience) when you can just put a start year ? You are going to have to update your data very often (best case once a year, that's assuming they are all born on the same day lol) in it's current state. That means recompiling the program (as your data is hard coded).