ENIGMA Forums

Outsourcing saves money => Issues Help Desk => Topic started by: RetroX on January 15, 2011, 04:46:52 PM

Title: Combining members
Post by: RetroX on January 15, 2011, 04:46:52 PM
Code: [Select]
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:
Code: [Select]
foobar val;
val.x = 5;
results in:
Code: [Select]
error: request for member ‘x’ is ambiguous
error: candidates are: int bar::x
error:                 int foo::x
Title: Re: Combining members
Post by: Josh @ Dreamland on January 15, 2011, 04:50:55 PM
Have both foo and bar inherit from a base class with X.
Title: Re: Combining members
Post by: Rusky on January 15, 2011, 06:55:54 PM
@Josh: that doesn't work if you really want two x's.

You can disambiguate val.x in the class with a using statement:
Code: (C) [Select]
struct foo { int x; };
struct bar { int x; };
struct foobar : public foo, bar { using foo::x; };

Then:
Code: (C) [Select]
foobar val;
val.x = 5; // x refers to foo::x
Title: Re: Combining members
Post by: Josh @ Dreamland on January 15, 2011, 07:03:18 PM
He just said he wanted a foobar::x. He didn't say he wanted to borrow one of the x's.
Title: Re: Combining members
Post by: Rusky on January 15, 2011, 07:26:59 PM
That creates a foobar::x. Although now that I actually read the topic title, he did say "combining..." Oops.
Title: Re: Combining members
Post by: RetroX on January 15, 2011, 08:20:29 PM
Yeah, the point of combining them was to force both classes to use the same value.  Anyways, thanks for the help.
Title: Re: Combining members
Post by: Rusky on January 16, 2011, 02:43:31 PM
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:

Code: (C) [Select]
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
Title: Re: Combining members
Post by: freezway on January 17, 2011, 03:17:43 PM
first read this and thought:

JoshDreamX or RetroLand... or maybe JoshAvatar...
Title: Re: Combining members
Post by: RetroX on January 18, 2011, 09:52:23 AM
what
Title: Re: Combining members
Post by: Fede-lasse on January 18, 2011, 10:21:45 AM
@freezway
Same.
Title: Re: Combining members
Post by: Josh @ Dreamland on January 18, 2011, 10:31:58 AM
I'll have you know it's JoshmAvaland.