How do I make a class instance a member of a parent class?

I have a class called world, of which I have 1 or 2 instances. Each of these classes contains a bunch of community class instances. At the moment I have these connected via pointers:

1
2
3
4
5
6
7
8
9
class world {
  vector<community*> members; // communities belonging to this world

  // bunch of variables here that each community needs access to
}

class community {
  world* pW; // pointer to the world that this community belongs
}


I find this rather cumbersome, and also daunting as I have many brain instances in a community, and many neuron instances in a brain.

I have looked up inheritance, but it seems to me like some construct like class world: public community would just achieve a situation where every instance of a community simply contains all members of a world as well.

Any tips?
Last edited on
I don't think inheritance will help you here. What is it exactly you find cumbersome and daunting about it?
First off, you need to define the relationships between these things. Do you have (is-a) or (has-a) relationships. They are the 2 main ones in OOP.

It seems to me that a Brain (has-a) neuron, many of them actually. You could encapsulate a container of Neuron objects inside Brain if that makes sense.

If any (is-a) relationships exist such as, "a C++Community (is-a) Community", then you will look to inheritance.

In that case, Community would be your base/parent/superclass and C++Community would be derived from it.

OOP is really something you need to spend alot of time reading about. You also should looking into understanding UML because it is based on OOP. Polymorphism is excellent when you want multiple classes in you inheritance hierarchy to share various methods or in better terms, have a shared interface. It's not so easy to implement without understanding how it works though. There are a few rules that go along with it.

Good luck.
Last edited on
IceThatJaw: yes, I have a pointer pC that indicates the parent community of each brain, and a vector vector<brain*>* in the community that indicates the brains that belong to the community.

What I was after was some sort of command that would link the instance of a brain to an instance of a community, such that each brain has this-> access to any members of the parent community, without going through the pC pointer. My code is completely CPU critical so I'm scrounging, on the other hand I think such an approach would also be a lot cleaner, as I wouldn't have to constantly create these "tags".

Peter: thanks, it seems that way.
Wow, you are going a bit crazy with the pointers there. You could design your program better so that the vector doesn't have to be a pointer. Just pass it by reference instead. I mean, you're not allocating new vectors dynamically, are you?

If you want Brain to have access to Community you can make it a friend in Community, although it's not really good programming.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Community {
	friend class Brain;
	int x;  // this is private by default
};

class Brain {
public:	
        Brain() { 
		this->c.x = 3;	// accessing a private member!!!
	}                       // also, "this->" is not good programming!
private:                        // use unique identifiers instead!
        Community c;
};


This is not good programming, but it will work. : (

It defeats the whole purpose of OOP and your best bet is to just redesign you hierarchy so that it makes sense and hides information from the client.
Last edited on
Topic archived. No new replies allowed.