Sign in to follow this  
thedodgeruk

friend method, cant figure out how to impliment it

Recommended Posts

i have 2 class one of them needs a funtion from the other
[code]

#pragma once
#include "Resource.h"
#include "Defines.h"

class CMeshManager
{
map <string,SMesh*> m_meshMap;
map<string,SMesh*> ::iterator m_it;
public:


SMesh* GetMesh(string filename); // THIS IS THE ONE
CMeshManager(void);
~CMeshManager(void);

public:
bool AddMeshFile(const string& fileName, SShader* exampleTechnique , bool tangents /*= false*/ );
int AddMeshFolder(string filename);
};

[/code]


[code]

#pragma once
#include "Entity.h"
#include "Defines.h"
#include "Camera.h"
#include "Model.h"
#include "Entity.h"
class CMeshManager;

class CEntityManager
{


public:
void UpdateAll(float frameTime);

deque <CModel*> GetModelList() { return m_modelList;}
deque <CEntity*> GetEntityList() { return m_entityList;}
deque <CCamera*> GetCameraList() { return m_cameraList;}
CEntityManager(void);
~CEntityManager(void);

friend class CMeshManager;
SMesh* GetMesh(string filename); /// freind class from meshmanager ?? not know how to implimet it
//interface
public:

CEntity* AddEntity();
CModel* AddModelEntity(string name );
CCamera* AddCamera();

CEntity* AddLightPoint(string name ,SShader* tech);
CEntity* AddLightSpot (string name ,SShader* tech);
CEntity* AddLightDirection();
};


[/code]


i have a funtion in meshmanager , but i need to be able to acces it from inside CentityManager

can some one tell me how this is done

Share this post


Link to post
Share on other sites
The Entity Manager can have a pointer to the Mesh Manager. You can add a constructor to the Entity Manager that takes a pointer to a Mesh Manager. I don't know why you want "friend" here, the function in question is (and should be) public.

As a side note, you almost certainly don't want to be returning Standard C++ Containers by value.

Share this post


Link to post
Share on other sites
In the code snippet in the OP, the friend declaration allows CMeshManager to access the protected / private parts of CEntityManager. Both classes have an own GetMesh(string) routine, and both provide them to the public. Using friend is hence not meaningful.

What you seem to need is access to an object of kind CMeshManager inside CEntityManager. Hence you need a pointer (or reference) to an object of type CMeshManager be known in an object of type CEntityManager. This can be accomplished by specifying it at initialization and storing it in a member variable:
[code]
class CEntityManager {
public:
CEntityManager(CMeshManager* meshManager);
...
private:
CMeshManager* _meshManager;
};

CEntityManager::CEntityManager(CMeshManager* meshManager)
: _meshManager(meshManager) {}

void
CEntityManager::doSomethingWithAMesh() {
SMesh* myMesh = _meshManager->GetMesh();
}
[/code]

(BTW: There are also other issues with the code snippets in the OP.)


EDIT: okay, rip-off has beaten me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this