Archived

This topic is now archived and is closed to further replies.

Zimans

Classes, structure

Recommended Posts

Zimans    237
I''ve started coding my first app that uses classes, and not having much expirience with classes I''ve come to this problem: I have my subsystems divided up into individual classes. The fileSystem is one class, the renderer is another class, The inout system is a class, etc. My problem is linking them all together. For instance, the View class (wich loads and manages the map) needs to be able to access the AI class so that it can lookup offset-for-name for scripts, and the input system needs to be able to tell the AI system "an input occured" Usually in a C app, wich i''m used to, i would just make all the subsystems in there own c file, and make the required functions global, so that any other subsys could call them.. What I''ve done so far is just have a func as part of a class that allows me to "attach" another class to it. For example, I attach the file sys to the renderer, then attach the View to the renderer.. Since I''m relatively new to classes, expirimentation will probably teach me the most, but any yips any of you have would be appreciated. Thx.

Share this post


Link to post
Share on other sites
DDnewbie    122
Hello,

I think ''friends'' is what you''re looking for.
For instance, if you want a function or class to have access to another class, then make it a friend. Now the friend-class/func can access every member in the other class, it''s like a member.

Note though, friends are usually a sign of design error.

Did you get it? If not, search for info on friends.

Bye,


/Mankind gave birth to God.

Share this post


Link to post
Share on other sites
Lord Pi    122
quote:
Original post by Zimans

I''ve started coding my first app that uses classes, and not having much expirience with classes I''ve come to this problem:

I have my subsystems divided up into individual classes. The fileSystem is one class, the renderer is another class, The inout system is a class, etc. My problem is linking them all together. For instance, the View class (wich loads and manages the map) needs to be able to access the AI class so that it can lookup offset-for-name for scripts, and the input system needs to be able to tell the AI system "an input occured"



Well, I''m not going to go into System Architecture, but from the sounds of it you might want to rethink the way your classes interact. Ideally, you''d want your classes to fall in layers, where all the classes in a single layer only communicate with those below and/or at the same level.

Note, I said ideally since this is not an easy thing to do. It takes a lot of experience (and confidence) in using classes before proceeding along this path. The only reason I brought this up was that the View class seems to be ''above'' the AI class, and I''m not used to that. If it works for you, then nevermind this bit of rambling.

Anyways... I''ll try to provide assistance in the area you were asking.

quote:

Usually in a C app, wich i''m used to, i would just make all the subsystems in there own c file, and make the required functions global, so that any other subsys could call them.. What I''ve done so far is just have a func as part of a class that allows me to "attach" another class to it. For example, I attach the file sys to the renderer, then attach the View to the renderer..



Hmmm... You should look into the Singleton design pattern, as it would help in this situation. It is used in cases where 1 (and only 1) instance of a class should exist [and be easily accessible].

In your h file:
class Singleton
{
public:
static Singleton *getInstance()
{
if (NULL == _instance)
_instance = new Singleton();
}
return _instance;
}
private:
static Singleton *_instance;
};

Then in your cpp file:
Singleton *Singleton::_instance = NULL;

Then if your Singleton had a class member Foo(), you''d access it
through:
Singleton::getInstance()->Foo();

Although, registering classes isn''t bad at all. Sometimes the singleton DP seems ''less clean,'' but it''s better than having global function, variables, or whatnot.

So based off your description, the renderer would be at the top level, calling down to the View and the filesystem? Hmmm...

quote:

Since I''m relatively new to classes, expirimentation will probably teach me the most, but any yips any of you have would be appreciated. Thx.


You''re right about experimenting. So experiment.


Dark Lord Pi

Share this post


Link to post
Share on other sites