Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 27th, 2006, 01:35 PM
Alex
Guest
 
Posts: n/a
Default Relationship between Classes


Curently, I am programing two Thread classes that are each other
members, class Communicator and Maintainer. These are their headers:

..... class Maintainer: public JThread, Object {

private:

Communicator* parent;
bool isRunning;
bool shouldContinue;
int timeout;

public:

Maintainer(Communicator* com);
void run();


};


....

class Communicator: public JThread{

Maintainer* maintainer;
JString configuration;

...

public:

Communicator(JString name, JString cnsHost, int cnsPort);
bool init();
bool connectToServer();
...


};

(JThread and JString are members of library that is included in this
project.)

I really need your advice about this, because output gives be some
strange errors and I am not really sure is this construction allowed
like this.
Maintainer object is created in init() function in object Communicator
and it receives object Communicator in constructor and keeps it as its
member,

maintainer = new Maintainer(this);

because Maintainer later checks some parameters of class Communicator
in run() function.
So Maintainer is member of Communicator and in same time, Communicator
ih member of Maintaner. It is really nessesary.

Is this allowed?




  #2  
Old March 27th, 2006, 02:15 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Relationship between Classes

On 27 Mar 2006 05:18:26 -0800, "Alex" <aleksandra.cerekovic@gmail.com>
wrote:
[color=blue]
>
>Curently, I am programing two Thread classes that are each other
>members, class Communicator and Maintainer. These are their headers:
>
>.... class Maintainer: public JThread, Object {
>
> private:
>
> Communicator* parent;
> bool isRunning;
> bool shouldContinue;
> int timeout;
>
> public:
>
> Maintainer(Communicator* com);
> void run();
>
>
> };
>
>
>...
>
> class Communicator: public JThread{
>
> Maintainer* maintainer;
> JString configuration;
>
> ...
>
> public:
>
> Communicator(JString name, JString cnsHost, int cnsPort);
> bool init();
> bool connectToServer();
> ...
>
>
> };
>
>(JThread and JString are members of library that is included in this
>project.)
>
>I really need your advice about this, because output gives be some
>strange errors and I am not really sure is this construction allowed
>like this.
>Maintainer object is created in init() function in object Communicator
>and it receives object Communicator in constructor and keeps it as its
>member,
>
>maintainer = new Maintainer(this);
>
>because Maintainer later checks some parameters of class Communicator
>in run() function.
>So Maintainer is member of Communicator and in same time, Communicator
>ih member of Maintaner. It is really nessesary.
>
>Is this allowed?[/color]

Yes. However, you need a forward declaration before class declaration
of Maintainer:

class Communicator;
class Maintainer: public JThread, Object {
private:
Communicator* parent;
// etc.

Then it should work OK. As long as you have a pointer or reference
member, you can do a forward declaration like this.

--
Bob Hairgrove
NoSpamPlease@Home.com
 

Bookmarks

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 On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.