ENIGMA Forums
Outsourcing saves money => Issues Help Desk => Topic started by: RetroX on January 15, 2011, 04:46:52 pm
-
struct foo { int x; };
struct bar { int x; };
struct foobar : public foo, public bar {};
results in a class with the members foo::x and bar::x. However, foobar::x does not exist. Is there any way to do this?
Example:
foobar val;
val.x = 5;
results in:
error: request for member ‘x’ is ambiguous
error: candidates are: int bar::x
error: int foo::x
-
Have both foo and bar inherit from a base class with X.
-
@Josh: that doesn't work if you really want two x's.
You can disambiguate val.x in the class with a using statement:
struct foo { int x; };
struct bar { int x; };
struct foobar : public foo, bar { using foo::x; };
Then:
foobar val;
val.x = 5; // x refers to foo::x
-
He just said he wanted a foobar::x. He didn't say he wanted to borrow one of the x's.
-
That creates a foobar::x. Although now that I actually read the topic title, he did say "combining..." Oops.
-
Yeah, the point of combining them was to force both classes to use the same value. Anyways, thanks for the help.
-
In that case, make sure that the shared base class is inherited virtually, or else foo and bar will each have their own copy of it anyway:
struct x { int x };
struct foo : virtual public x {};
struct bar : virtual public x {};
struct foobar : public foo, bar { /* may need using x::x */ };
foobar val;
val.x = 5; // foo and bar both see this change
-
first read this and thought:
JoshDreamX or RetroLand... or maybe JoshAvatar...
-
what
-
@freezway
Same.
-
I'll have you know it's JoshmAvaland.