Multiple Inheritance - v2

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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 :)
Last edited on
closed account (o1vk4iN6)
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).
Last edited on
This is a horrendous misuse of inheritance.

Just do the simple approach and make these all members:

1
2
3
4
5
6
7
8
class Beatle
{
private:
  int guitarExperience;
  int singingExperience;
  int drumExperience;
  ...
};


KISS
Topic archived. No new replies allowed.