创建一种c++接口
Create a kind of Interface c++
我正在写一个小的2d渲染框架与管理器的输入和资源,如纹理和网格(2d几何模型,如四边形),他们都包含在一个类"引擎",与他们和directX类交互。每个类都有一些公共方法,比如init或update。它们由引擎类调用来渲染资源,创建资源,但其中很多不应该由用户调用:
//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
}
通过这种方式,用户调用Engine::GetTmanager将有权访问Tmanager的所有公共方法,包括init update和rendertexture,这些方法必须仅由Engine在其init, render和update函数中调用。那么,以以下方式实现用户界面是个好主意吗?
//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;
通过这种方式,i可以为每个对象实现多个接口,只保持i(和用户)需要的函数可见。
有更好的方法做同样的事吗??或者它只是无用的(我没有隐藏"框架私有函数",用户将学会不调用它们)?
在我使用这个方法之前:
class manager
{
public:
//engine functions
userfunction();
}
class engine
{
private:
manager m;
public:
init(){//call manager init function}
manageruserfunciton()
{
//call manager::userfunction()
}
}
这样我就不能访问管理器类了但这是一种不好的方式,因为如果我添加我需要在引擎类中添加一个新方法,这需要花费很多时间。
对不起,我的英语不好。
您可以使用您的方法,但是最好从公共接口派生私有接口:
class TexManagerPublic
{
public:
virtual void load() = 0;
virtual void save() = 0;
};
class TexManagerPrivate : public TexManagerPublic
{
public:
void load();
void save();
void init();
void update();
};
class Engine
{
TexManagerPrivate theTexManager;
public:
Engine() { theTexManager.init(); }
TexManagerPublic* texManager() { return &theTexManager; }
};
或者你可以让一个'Engine'成为TextureManager的朋友,像这样:
class TexManager
{
private:
void init();
void update();
friend class Engine;
};
class Engine
{
TexManager texManager;
void init()
{
texManager.init();
}
};
相关文章:
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 有没有一种方法可以在编译时获得作用域类名
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- 一种在C++中读取TXT配置文件的简单方法
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 有没有一种优雅而快速的方法来测试整数中的 1 位是否位于连续区域
- 在运行时检查继承是否只有一种类型和 void*
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- 寻找一种更好的方法来表示无符号字符数组
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量