Pages: [1]
  Print  
Author Topic: Combining members  (Read 1425 times)
Offline (Male) RetroX
Posted on: January 15, 2011, 04:46:52 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
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
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: January 15, 2011, 04:50:55 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2949

View Profile Email
Have both foo and bar inherit from a base class with X.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #2 Posted on: January 15, 2011, 06:55:54 PM

Resident Troll
Joined: Feb 2008
Posts: 961
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
@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
Logged
Offline (Male) Josh @ Dreamland
Reply #3 Posted on: January 15, 2011, 07:03:18 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2949

View Profile Email
He just said he wanted a foobar::x. He didn't say he wanted to borrow one of the x's.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #4 Posted on: January 15, 2011, 07:26:59 PM

Resident Troll
Joined: Feb 2008
Posts: 961
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
That creates a foobar::x. Although now that I actually read the topic title, he did say "combining..." Oops.
Logged
Offline (Male) RetroX
Reply #5 Posted on: January 15, 2011, 08:20:29 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Yeah, the point of combining them was to force both classes to use the same value.  Anyways, thanks for the help.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #6 Posted on: January 16, 2011, 02:43:31 PM

Resident Troll
Joined: Feb 2008
Posts: 961
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
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
« Last Edit: January 16, 2011, 02:45:09 PM by Rusky » Logged
Offline (Unknown gender) freezway
Reply #7 Posted on: January 17, 2011, 03:17:43 PM

Member
Joined: Dec 2009
Posts: 220

View Profile
first read this and thought:

JoshDreamX or RetroLand... or maybe JoshAvatar...
Logged
if you drop a cat with buttered toast strapped to its back, which side lands down?
joshdreamland: our languages are based on the idea that it's going to end up FUBAR
/kick retep998
Offline (Male) RetroX
Reply #8 Posted on: January 18, 2011, 09:52:23 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
what
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Post made January 18, 2011, 10:21:45 AM was deleted at the author's request.
Offline (Male) Josh @ Dreamland
Reply #10 Posted on: January 18, 2011, 10:31:58 AM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2949

View Profile Email
I'll have you know it's JoshmAvaland.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Pages: [1]
  Print