Create a kind of Interface c++ [migrated]
- by Liuka
I'm writing a little 2d rendering framework with managers for input and resources like textures and meshes (for 2d geometry models, like quads) and they are all contained in a class "engine" that interacts with them and with a directX class. So each class have some public methods like init or update. They are called by the engine class to render the resources, create them, but a lot of them should not be called by the user:
//in pseudo c++
//the textures manager class
class TManager
{
private:
vector textures;
....
public:
init();
update();
renderTexture();
//called by the "engine class"
loadtexture();
gettexture();
//called by the user
}
class Engine
{
private:
Tmanager texManager;
public:
Init()
{
//initialize all the managers
}
Render(){...}
Update(){...}
Tmanager* GetTManager(){return &texManager;} //to get a pointer to the manager
//if i want to create or get textures
}
In this way the user, calling Engine::GetTmanager will have access to all the public methods of Tmanager, including init update and rendertexture, that must be called only by Engine inside its init, render and update functions.
So, is it a good idea to implement a user interface in the following way?
//in pseudo c++
//the textures manager class
class TManager
{
private:
vector textures;
....
public:
init();
update();
renderTexture();
//called by the "engine class"
friend class Tmanager_UserInterface;
operator Tmanager_UserInterface*(){return reinterpret_cast<Tmanager_UserInterface*>(this)}
}
class Tmanager_UserInterface : private Tmanager
{
//delete constructor
//in this class there will be only methods like:
loadtexture();
gettexture();
}
class Engine
{
private:
Tmanager texManager;
public:
Init()
Render()
Update()
Tmanager_UserInterface* GetTManager(){return texManager;}
}
//in main function
//i need to load a texture
//i always have access to Engine class
engine-GetTmanger()-LoadTexture(...) //i can just access load and get texture;
In this way i can implement several interface for each object, keeping visible only the functions i (and the user) will need.
There are better ways to do the same?? Or is it just useless(i dont hide the "framework private functions" and the user will learn to dont call them)?
Before i have used this method:
class manager
{
public:
//engine functions
userfunction();
}
class engine
{
private:
manager m;
public:
init(){//call manager init function}
manageruserfunciton()
{
//call manager::userfunction()
}
}
in this way i have no access to the manager class but it's a bad way because if i add
a new feature to the manager i need to add a new method in the engine class and it takes a lot of time.
sorry for the bad english.