Pages: [1]
  Print  
Author Topic: Stuff referencing each other.  (Read 1338 times)
Offline (Male) retep998
Posted on: July 10, 2010, 03:34:59 PM

Member
Location: Where else?
Joined: Jan 2010
Posts: 248
MSN Messenger - retep998@charter.net AOL Instant Messenger - retep998 Yahoo Instant Messenger - retep998
View Profile Email
Suppose I have this c++ code.
How should I mess with definitions to get this working?
Code: [Select]
class foo { bar opposite(); };
class bar { foo opposite(); };
Logged
Offline (Male) Rusky
Reply #1 Posted on: July 10, 2010, 03:42:26 PM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Code: [Select]
class foo { class bar opposite(); };
class bar { foo opposite(); };
Logged
Offline (Unknown gender) luiscubal
Reply #2 Posted on: July 10, 2010, 05:18:42 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Code: [Select]
class bar;
class foo { bar opposite(); }
class bar { foo opposite(); }

Disclaimer: I have not tested this. It might give an incomplete type error.
Logged
Offline (Male) RetroX
Reply #3 Posted on: July 10, 2010, 09:31:08 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Code: [Select]
class bar;
class foo { bar opposite(); }
class bar { foo opposite(); }

Disclaimer: I have not tested this. It might give an incomplete type error.
Doesn't work.  At least, not on strict.  I believe that the GCC has a flag for this sort of thing.
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 #4 Posted on: July 10, 2010, 11:33:31 PM

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

View Profile Email
Returning incomplete types seems sketchy to me. Try returning a pointer, that's guaranteed to never fail, however incomplete. But I'm not sure, since you're not implementing opposite() immediately, why it wouldn't work.
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 (Unknown gender) luiscubal
Reply #5 Posted on: July 11, 2010, 09:45:02 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Quote
Try returning a pointer, that's guaranteed to never fail, however incomplete.
However, pointers can be problematic for memory leaks/premature free()s reasons.
One possible solution would be:
Code: [Select]
class bar;
class foo {
void opposite(bar& result);
friend bar do_foo_opposite(foo* self);
}

class bar { foo opposite(); }

bar do_foo_opposite(foo* self) {
  bar b;
  self->opposite(b);
  return b;
}
Logged
Offline (Male) Rusky
Reply #6 Posted on: July 11, 2010, 11:22:23 AM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Guys.
Code: [Select]
class foo { class bar opposite(); };
class bar { foo opposite(); };
That will work so long as you implement opposite() after both declarations.
Logged
Pages: [1]
  Print