Connecting Tech Pros Worldwide Help | Site Map

Relationship between Classes

  #1  
Old March 27th, 2006, 01:35 PM
Alex
Guest
 
Posts: n/a

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Relationship between multiple diamonds Immortal Nephi answers 2 November 7th, 2008 09:55 PM
how to create relationship between two IList collection classes prakashbpl1 answers 2 November 3rd, 2008 12:29 PM
Abstract Classes and Interfaces relationship between C# and VB.Net =?Utf-8?B?UmljaA==?= answers 5 December 7th, 2007 11:15 PM
few questions concerning classes alternativa answers 7 April 30th, 2006 07:25 PM