Connecting Tech Pros Worldwide Forums | Help | Site Map

Relationship between Classes

Alex
Guest
 
Posts: n/a
#1: Mar 27 '06

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?


Bob Hairgrove
Guest
 
Posts: n/a
#2: Mar 27 '06

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
Closed Thread