PDA

View Full Version : Two parents should know the same subclass



JSoftware
24-02-2005, 05:28 AM
i have a weird problem.. i'm making a new design to my game engine but this design needs to be more object oriented than the last one i scrapped so i want my texturemanager and modelmanager to have access to a virtual file system class. but how can i make that these classes have access to the same instance of this filesystem? they are two child classes which are owned by another "higher class"..

:? -HELP-...

Regards Jeppe

cairnswm
24-02-2005, 07:36 AM
To share an instance of the class indicates that they are not children of that class but that there are several different objects being used.


TextureManager := TTextureManager.Create;
LightingManager := TLightingManager.Create;

FileSystem := TFileSystem.Create;
TextureManager.FileSystem := FileSystem;
LightingManager.FileSystem := FileSystem;


This means that a FileSystem is created and each of the other objects can use the information available in that FileSystem Object.

JSoftware
24-02-2005, 07:52 AM
Should the filesystem variable in texturemanager and modelmanager then be a pointer or a variable?

well i'll try that! Thanks :D

regard Jeppe

marcov
24-02-2005, 09:12 AM
Should the filesystem variable in texturemanager and modelmanager then be a pointer or a variable?

well i'll try that! Thanks :D

regard Jeppe

A "class" already is a reference (read: pointer).

The trick if multiple classes have references to the same class is to think (and document/annotate) clearly which class "owns" the object for e.g. freeing purposes.

So if X,Y,Z both reference A, we specify that e.g. X also OWNS A, so when X gets destroyed, in its destructor it free's A.