Children Accessing Parents?

Started by
15 comments, last by Volte6 19 years, 1 month ago
Yeah i'm trying to restructure things to be much more managed... essentially every class has a class that manages it.

Game->Video
Game->SpriteListManager->SpriteList->Sprite

The thing is, my code starts falling apart when I have too many layers of parents->children... I always end up witha child wanting to access something it really doesn't have access to... because it's 3 tiers back and one over in the hierarchy or something.
Advertisement
Quote:Original post by Volte6
The thing is, my code starts falling apart when I have too many layers of parents->children... I always end up witha child wanting to access something it really doesn't have access to... because it's 3 tiers back and one over in the hierarchy or something.


Well a derived class should be able to access it's parent, and if you need it to access something it's not linked to then have the methods take a reference to that something. There is no need for a singleton.

class CA{ public:  int i;};class CB{ public:  void Modify(CA& object, int n)  {    object.i = n;  }};
- A momentary maniac with casual delusions.
Try reading up on "design patterns". DPs lay out some practices and principals for handling typical situations in object-oriented software design. Here's a good start.
Quit screwin' around! - Brock Samson
Quote:Original post by coderx75
Here's a good start.


Excellent!
- A momentary maniac with casual delusions.
Quote:Original post by JohnBolton
It creates a circular dependency -- the parent class depends on the child class (which is normal), but the child class also depends on the parent class. The relationship is no longer really parent/child, master/slave, server/client, etc.


It's true, folks, programmers are naturally kinky SOBs ;) (heck... I expected this to be about base and derived classes, and keyword 'protected'... which leads to a whole other set of innuendo... but I think I won't get into that :) )
I think he's basically sayings that while a child can access the parent, at the time it does so it no longer is a full child. I have a couple horrible analogies, but I'll go with a decent one of "employer"/"butler" If an employer needs something small he requests it from the butler, but a butler doesn't go to the employer when he needs to do something.

That's not to say Circular dependancies are bad, but when two classes call each others functions it's not really a child and parent, it's more of a equal system. This is not a bad thing. On my program I thought I'd have the great lords of my game, four thread classes, (networking, engine, Gamelogic, and Rendering/IO) with engine as the lord of all. Well now I also have the possibility of Audio as a thread class, Engine needs input from all the classes, (especially to quit) and the graphics module has it's two children upgraded to equals, who then access just about everything. So it's not something to worry about.

As someone meantioned the solution to the problem is probably
class A;class B{public: A Imok;};class A{public:B Imoktoo;};


While this looks like a redefinition, it's more along the lines of defineing the interface (is that the correct word?) for a class in the Header of a file, then defining the actual functions in a .cpp file.

Of course when your using this methods, pointers are almost essential for both.

class A;class B{public: A *Imok;};class A{public:B *Imoktoo;};
Me again!

Okay, couple thangs:

1.) Why would pointers be essential?
[EDIT: I just realized. n/m on this one]

2.) I tried this... it worked until I tried to access a method of the parent... IE:
// child.hclass Parent;class Child{  Parent *parent;  Child(Parent *pParent)  {    parent = pParent;  }  void commandParent()  {    parent->grumble();  }};// parent.hclass Parent{  Child *pLeech;  Parent()  {    pLeech = new Child(this);  }};


This invariably gives me something like:
error C2027: use of undefined type 'Parent'

[Edited by - Volte6 on March 15, 2005 12:25:39 PM]

This topic is closed to new replies.

Advertisement