Multiple inheritance problem

Pages: 12
Mar 1, 2012 at 7:37pm
I lay code first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct foo1
{
    float x, y, z;
};

struct foo2
{
    float x, y;
};

struct fooN...// and lot of other foo, although they all contain PODs

template< class... T >
class Bar : public T... {};

typedef Bar<foo1, foo2, foo3> specificBar1;
typedef Bar<foo2, foo4>       specificBar2;
typedef Bar<foo1, foo3, foo9> specificBar3;
...
specificBar1 bar;
...
bar.x = 0.1f;// I want to set foo1 'x' ??? 


How to differentiate witch 'x' to set?

Thanks for your time.
Mar 1, 2012 at 7:39pm
This doesn't look as if it's a problem with multiple inheritance, but rather a problem with understanding polymorphism...
Mar 1, 2012 at 7:40pm
Ok, whatever.
How to resolve this?
Mar 1, 2012 at 7:43pm
RTFM
Mar 1, 2012 at 7:49pm
I first used search with google, but could not find answer as i don't know exact keywords to search where members have same names. And i don't want to change their names.

BTW. Did i do something wrong to upset you?
If you don't want to help you don't need to spam my thread!
Mar 1, 2012 at 7:52pm
a problem with understanding polymorphism...


Ok, whatever.


Don't "ok, whatever" someone that's trying to help you. I was basically giving you exactly what you needed to research in order to try to figure out how to resolve what this appears to be faulting at, and your only response was "ok, whatever. Fix it for me."
Mar 1, 2012 at 8:01pm
Well i searched with "multiple", "inheritance", "polymorphism"... and similar, but could not find answer to resolve issue if members have same names.

What is the purpose of this forum?
Last edited on Mar 1, 2012 at 8:02pm
Mar 1, 2012 at 8:05pm
Your problem isn't that your members have the same name, it's that you need a paradigm shift completely away from multiple inheritance and into polymorphism.

http://www.cplusplus.com/doc/tutorial/polymorphism/
Mar 1, 2012 at 8:13pm
So you say it can't be done? I don't want to change "paradigm" if possible!
Last edited on Mar 1, 2012 at 8:13pm
Mar 1, 2012 at 8:15pm
Short answer:

bar.foo1::x = 0.1f

Short question:

Why would you want to do that? Polymorphism doesn't require this. You'd normally pass bar to functions that work with foo1's or foo2's respectively, so you should seldom get into the situation where such a problem arises.

Mar 1, 2012 at 8:15pm
Then don't.
Mar 1, 2012 at 8:24pm
Thanks hanst99.


You'd normally pass bar to functions that work with foo1's or foo2's respectively...


If it isn't much to ask for small example? Please, if you find time?
Never mind, i see what you mean:
1
2
3
4
5
6
7
8
9
void set_foo1(foo1& f)
{
   f.x = 0.0f;
...
 ...
}

...
set_foo1(bar);


@ciphermagi
I am sorry if i have done something to upset you. I don't know why are you mad at me?

@ALL
I don't see why multi-inheritance is bad here?
Last edited on Mar 1, 2012 at 8:47pm
Mar 1, 2012 at 8:52pm
Hi

@morando
Could your write us what kind of a problem you want to solve, and why you should use such structure ?

It is better to write the actual problem, the goal, and than your code. Without knowing what you really want to solve or what you want your code to do, no one could be able to help you well.

It is seems to me that you have some Diamond shaped inheritance problems which causes ambiguity, which in turn can be terminated by using inheritance through virtual Base Classes or by using the scope operator :: . Just the way hanst99 suggested you.


Hope it helps

Mar 1, 2012 at 9:01pm
Ok. I have also started to learn DirextX API, and i need some vertex data structures to build various vertex types, so i thought to use variadic template & multiple-inheritance:

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
struct position
{
    float x, y, z;
};

struct normal
{
    float x, y, z;
};

struct tangent
{
    float x, y, z;
};

struct binormal
{
    float x, y, z;
};

struct tex_coord
{
    float u, v;
};

template< class ...T >
struct vertex : public T...
{
};

typedef vertex<position, normal, tex_coord>          test_vertex1;
typedef vertex<position, normal, tangent, tex_coord> test_vertex2;
Last edited on Mar 1, 2012 at 9:03pm
Mar 1, 2012 at 9:05pm

I am sorry if i have done something to upset you. I don't know why are you mad at me?


You're no native english speaker, are you? The way you worded it, it sounded as if you were just brushing off his answer.


It is better to write the actual problem, the goal, and than your code. Without knowing what you really want to solve or what you want your code to do, no one could be able to help you well.


Actually, he made it pretty clear what he wanted to do. And there is a definite solution to the problem. Why anyone would use such a structure is beyond the scope of the topic, and I'd guess his actual code looks a little bit different. After all, there are valid (even good) reasons to use multiple inheritance, and name collisions do happen.
Mar 1, 2012 at 9:08pm

You're no native english speaker, are you? The way you worded it, it sounded as if you were just brushing off his answer.

Yes, i am not. My bucket of words is limited, so i don't know if i worded something wrongly.
Mar 1, 2012 at 9:14pm

Yes, i am not. My bucket of words is limited, so i don't know if i worded something wrongly.


Don't take it to heart. Quite a few people here - including me - aren't.
Mar 1, 2012 at 9:41pm
Name collisions? In C++? I've never heard of such a thing. The scope resolution operator is very powerful, as well as the using directive.

See how powerful they are, even in the face of polymorphic classes:
http://ideone.com/coOPY
Haha at the url, C OOP
Mar 1, 2012 at 9:42pm
¿why does 'test_vertex1' don't have 'tangent'?
It seems an scenario more adequate for composition.
1
2
3
4
5
6
7
8
9
10
struct vector3f{
  //float x[3]; //I prefer this
  //std::valarray<float> x; //or better, it has implemented operators. (manage the size in a constructor)
  float x,y,z;
};

struct test_vertex1{
  vector3f position, normal;
  vector2f tex_coord;
};
Mar 1, 2012 at 9:45pm
Name collisions? In C++? I've never heard of such a thing. The scope resolution operator is very powerful, as well as the using directive.


There are name collisions, using the scope operator to resolve them doesn't change that.
Pages: 12