Can attributes be virtual like methods in polymorphism ?

Hi all, all is in the title.
no
Thanks Peter87. So, to go a bit further, what is the good paradigm please for something like this (pseudo-code) :

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
class Data57_1 {
	int a[2];
};
class Data57_2 {
	int a[2];
	float b[3];
};
class C57 {
public:
	virtual int show_data() =0;
protected:
	virtual X_TYPE data =0;
};
template <int n>
class D57 : public C57 {
public:
	int show_data() {
		cout << "a[0] = " << a[0] << endl;
		if (n == 2) cout << "b[0] = " << b[0] << endl;
	}
protected:
	if (n == 1) X_TYPE is Data57_1;
	if (n == 2) X_TYPE is Data57_2;
	X_TYPE data;
};


In other words, I need to have derived classes that are differentiated not only by methods, but mainly by attributes - I mean the data stored. More precisely, the data will be tables of structures containing more data in some derived classes than in others. But the methods are mostly the same.
Last edited on
I have reached to this :

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
struct Data57_1 {
	int a[2];
};
struct Data57_2 {
	int a[2];
	float b[3];
};
class C57 {
public:
	virtual int show_data() =0;
};
template <int n, class T>
class D57 : public C57 {
public:
	D57(int a0, int a1) {data.a[0] = a0; data.a[1] = a1;}
	int show_data() {
		cout << "a[0] = " << data.a[0] << endl;
		return 0;
	}
protected:
	T data;
};
template <>
class D57<2,Data57_2> : public C57 {
public:
	D57(int a0, int a1, float b0, float b1, float b2) {data.a[0] = a0; data.a[1] = a1; data.b[0] = b0; data.b[1] = b1; data.b[2] = b2;}
	int show_data() {
		cout << "a[0] = " << data.a[0] << endl;
		cout << "b[0] = " << data.b[0] << endl;
		return 0;
	}
protected:
	Data57_2 data;
};
//template <2,Data57_2> D57 : public C57;
//template D57<2,Data57_2> : public C57;
//template D57<2,Data57_2>;

int main(int argc, const char *argv[]) {
	D57<1,Data57_1> d1(11, 12);
	D57<2,Data57_2> d2(21, 22, 2.1, 2.2, 2.3);
	C57* p1 = &d1;
	C57* p2 = &d2;
	p1->show_data();
	p2->show_data();
              return 0;
}


What is your opinion ?
Last edited on
You can omit the template parameter n. It has no effect.

What about this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class C57 {
public:
	C57(int a0, int a1) {a[0] = a0; a[1] = a1;}
	virtual int show_data() {
		cout << "a[0] = " << data.a[0] << endl;
		return 0;
	}
protected:
	int a[2];
};

class D57 : public C57 {
public:
	D57(int a0, int a1, float b0, float b1, float b2) : C57(a0,a1) {b[0] = b0; b[1] = b1; b[2] = b2;}
	int show_data() {
		C57::show_data();
		cout << "b[0] = " << b[0] << endl;
		return 0;
	}
protected:
	float b[3];
};
Thanks coder777,
In my real world, I am constrained to use the data structures Data57_1 and Data57_2, because they come from elsewhere with big tables of these structures accessed via hash tables. So the data shall? stay in the derived class to differentiate if it is Data57_1 or Data57_2. For data integrity proof, I cannot separate the data in parallel hash tables.
Sorry, I should have provided the constraints.
Topic archived. No new replies allowed.