Can You Pass Class References to Constructor

Greetings All,

I have one class I'll call Main class and it creates and initializes all of the classes so that they are not re-created again. What I simply want to do is to have in the constructors of my other classes pointers to the classes they need to operate.

I get errors when I try, and I have looked in various forums. The question is, how is the proper way to do this, if it can/should be done. I don't want to have to pass each one for each function that needs it. Thank you in advance for any help.

here is the class *constructor* that creates the new classes Main
1
2
3
4
5
6
7
8
EcoAreaTeam*		m_pEcoAreaTeam = new  EcoAreaTeam();
LifePattern*		m_pLifePattern = new  LifePattern();
NeedTeam*			m_pNeedTeam  = new  NeedTeam();
SceneFrameTeam*		m_pSceneFrameTeam  = new  SceneFrameTeam(m_pEcoAreaTeam, m_pStatisticCalculation,
															m_pSymptomManager, m_pTrioTeam);
StatisticCalculation*   m_pStatisticCalculation  = new  StatisticCalculation();
SymptomManager*		m_pSymptomManager  = new  SymptomManager(); 
TrioTeam*			m_pTrioTeam  = new  TrioTeam();


here is a class that i trying to use to access pointers to other classes in the .cpp file

1
2
3
4
5
6
7
8
SceneFrameTeam::SceneFrameTeam(EcoAreaTeam* eco, StatisticCalculation* stat,
					  SymptomManager* sym, TrioTeam* trio):
						m_pEcoAreaTeam(eco),
						m_pStatisticCalculation(stat),
						m_pSymptomManager(sym),
						m_pTrioTeam(trio){}




here is the .h file creation information for SceneFrameTeam:
1
2
3
4
5
6
7
8
9
10
11
12
class   EcoAreaTeam;
class	SceneFrameBase;
class   StatisticCalculation;
class   SymptomManager;
class   TrioTeam;

//these are the pointers to the classes in the SceneFrameTeam .h
EcoAreaTeam*			m_pEcoAreaTeam; 
StatisticCalculation*	m_pStatisticCalculation;
SymptomManager*			m_pSymptomManager; 
TrioTeam*				m_pTrioTeam;


here is the constructor info in .h:
[CODE]
SceneFrameTeam(EcoAreaTeam* eco,
StatisticCalculation* stat,
SymptomManager* sym,
TrioTeam* trio);

[/code]

this is the error message:

error C2065: 'eco' : undeclared identifier


and

error C3867: 'SceneFrameTeam::EcoAreaTeam': function call missing argument list; use '&SceneFrameTeam::EcoAreaTeam' to create a pointer to member


the thing is I was looking at an example from Matt Buckland's AI book and he did not seem to need the &

I guess the question is, is there a simple way to do this?

Peace,

Marcia
Your problem seems to be a classic example for wanting to use the Singleton pattern. http://en.wikipedia.org/wiki/Singleton_pattern

But yes, you can pass a pointer to any other class into the constructor of another class. You just have to make sure you have the class declared at the point of implementing the constructor.
@ Zaita Thank you. Perhaps I was not clear

This is what I am doing:

(a) the first Mainclass creates all the classes. Two of the classes that it creates are: m_pArtClass = new ArtClass();

it also creates m_pSchoolClass = new SchoolClass();

(b) Now the thing is that in the SchoolClass, there are some functions where it needs to call the ArtClass(). For example:
m_pArtClass->Paint();

(c) So what I want to avoid is having to create a new ArtClass in the SchoolClass for it to access. I simply wanted the MainClass to create the SchoolClass() with a pointer to the ArtClass like this:

(d)
SchoolClass* m_pSchoolClass = new SchoolClass(m _pArtClass);

As you see the ArtClass() would be in the constructor for the SchoolClass to simply use, it would not need to create a *new* ArtClass and could simply use the one that was passed in

Thank you. I hope this is not too fuzzy
solved..I was not defining the class reference correctly. Peace :)
Topic archived. No new replies allowed.