LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-06-2011, 04:05 AM   #1
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Rep: Reputation: 24
C++ - Two-way Communication between objects if A (obj) creates an instance of B (obj)


I have two classes, for argument's sake A and B.

A implements the core functionality, B is an encapsulated data structure.

If you imagine this situation

Code:
class A
{
public:
    type function();
private:
    type variable;
    B object;
};
From within B's member functions, I would like to access the public function() in class A.

This is not an inheritance issue, they are two discrete classes with radically different functionality.

Class A makes an object of B.

If someone could please offer advice about the "best practise" way to set up this communication, I would greatly appreciate it!

I would prefer advice in the form of topics or links to reading material, rather than code samples!

Thank you!
 
Old 04-06-2011, 04:17 AM   #2
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,599
Blog Entries: 29

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
I may be barking up the wrong tree, but still, did you look up the "Friend" functionality?
 
Old 04-06-2011, 04:27 AM   #3
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Anisha Kaul View Post
I may be barking up the wrong tree, but still, did you look up the "Friend" functionality?
I considered that, however, the issue is how to communicate backward?

Send a pointer of "this" from A to B?
 
Old 04-06-2011, 04:31 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,599
Blog Entries: 29

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Did you look up the C++ "Callbacks"?
 
Old 04-06-2011, 04:55 AM   #5
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by Gavin Harper View Post
I considered that, however, the issue is how to communicate backward?

Send a pointer of "this" from A to B?
Yes, just make B accept an A* in its constructor, and then A can pass in its reference to itself when it constructs B. Just make sure that if A is ever going to disown its instance of B that you destroy B/nullify this pointer/pass it to a new A/etc. I would urge you not to consider friend classes though - for anything other than overloading certain operators, they're usually an indicator of bad design to start with.
 
Old 04-06-2011, 05:27 AM   #6
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by JohnGraham View Post
Yes, just make B accept an A* in its constructor, and then A can pass in its reference to itself when it constructs B. Just make sure that if A is ever going to disown its instance of B that you destroy B/nullify this pointer/pass it to a new A/etc. I would urge you not to consider friend classes though - for anything other than overloading certain operators, they're usually an indicator of bad design to start with.
Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] make: *** [.obj/matrix.o] Error ifonly Linux - Newbie 16 01-17-2012 05:04 AM
.c.asm.obj.exe jackall Linux - Newbie 2 11-20-2008 08:16 PM
gcc-obj install fail pingvina Linux - Software 1 08-12-2006 05:58 AM
looking for a .obj files viewer aizkorri Linux - Software 0 09-19-2003 05:23 AM
Using Shared Obj implicitly ugenn Programming 0 04-15-2002 09:14 AM


All times are GMT -5. The time now is 12:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration