标题有点用词不当,我的意思是"C with classes".
让我解释一下,最近我买了一本书ShaderX7,它带有一个关于阴影映射技术的文章的Unigine引擎的精简版(和旧版).
当我意识到,虽然作者使用的是C++和继承以及所有C++的优点,但是我正在探讨代码,大多数方法内容基本上都是C风格的代码; 例如:
int Shader::get_param(const char *name,char *src,String &dest) const {
char *s = src;
int size = (int)strlen(name);
while(*s) {
if(!strncmp(s,name,size)) {
if((s == src || strchr("\n\r",*(s - 1))) && strchr(" \t",*(s + size))) {
src = s;
s += size;
dest.clear();
while(*s && strchr(" \t",*s)) s++;
while(*s && strchr(" \t\n\r",*s) == NULL) dest += *s++;
if(dest.isEmpty()) {
Log::error("Shader::get_param(): can't get \"%s\" \n",name);
return 0;
}
memmove(src,s,strlen(s) + 1);
return 1;
}
}
s++;
}
return …Run Code Online (Sandbox Code Playgroud) 很抱歉很长的标题,但我确实想要具体.我希望以下代码可以工作,但它没有,我无法弄清楚原因:/
#include <cstdio>
#include <cassert>
class UniquePointer
{
public:
void Dispose()
{
delete this;
}
friend void SafeDispose(UniquePointer*& p)
{
if (p != NULL)
{
p->Dispose();
p = NULL;
}
}
protected:
UniquePointer() { }
UniquePointer(const UniquePointer&) { }
virtual ~UniquePointer() { }
};
class Building : public UniquePointer
{
public:
Building()
: mType(0)
{}
void SetBuildingType(int type) { mType = type; }
int GetBuildingType() const { return mType; }
protected:
virtual ~Building() { }
int mType;
};
void Foo() …Run Code Online (Sandbox Code Playgroud) 我正在一些图形API(DirectX9和DirectX11)上面写一个抽象层,我想你的意见.
传统上,我会为每个想要抽象的概念创建一个基类.
因此,在典型的OO方式中,我将有一个类Shader和2个子类DX9Shader和DX11Shader.
我会重复纹理等过程...当我需要实例化它们时,我有一个抽象工厂,它将根据当前的图形API返回适当的子类.
在RAII之后,返回的指针将封装在std :: shared_ptr中.
到目前为止一直很好,但就我而言,这种方法存在一些问题:
这促使我重新设计我的工作方式:我认为我可以返回资源的原始指针,并让图形API自行清理,但仍然存在客户端悬挂指针和接口问题的问题.我甚至认为手动引用计数像COM,但我认为这将是一个倒退(纠正我,如果我错了,来自shared_ptr世界,手动引用计数似乎是原始的).
然后我看到了Humus的工作,其中所有的图形类都由整数ID表示(很像OpenGL的作用).创建一个新对象只返回其整数ID,并在内部存储指针; 它完全不透明!
代表抽象的类(例如DX9Shader等......)都隐藏在设备API后面,这是唯一的接口.
如果想要设置纹理,只需调用device-> SetTexture(ID),其余的就在幕后发生.
失败的原因是API的隐藏部分膨胀,需要很多锅炉板代码才能使其工作,我不是一个全能类的粉丝.
任何想法/想法?
最近我看到一位同事FirstOrDefault()在与Stack交互时使用而不是Peek().
我从未想过使用扩展方法而不是内置方法Peek(),我想知道两者之间的含义/差异是什么.
一个推荐超过另一个?用ildasm.exe查看并没有教我任何有用的东西.
我在DLL中导出了一些导出到Luabind的类,一切都适用于这两个类(LuaScriptManager,EventManager).我可以从Lua调用它们的函数,一切都很好,但现在我正在尝试在我的客户端可执行文件中设置一个与DLL链接的新类,到目前为止还没有运气.
这是我为每个函数调用的错误消息:"找不到匹配的重载,候选:void loadResource(ResourceManager&,std :: string const&)"
类绑定来自http://www.nuclex.org/articles/5-cxx/1-quick-introduction-to-luabind:
struct Manager {
Manager() :
m_ResourceCount(0) {}
void loadResource(const std::string &sFilename) {
++m_ResourceCount;
}
size_t getResourceCount() const {
return m_ResourceCount;
}
size_t m_ResourceCount;
};
static Manager MyResourceManager;
void Bind(lua_State* l)
{
// Export our class with LuaBind
luabind::module(l) [
luabind::class_<Manager>("ResourceManager")
.def("loadResource", &Manager::loadResource)
.property("ResourceCount", &Manager::getResourceCount)
];
luabind::globals(l)["MyResourceManager"] = &MyResourceManager;
}
Run Code Online (Sandbox Code Playgroud)
这是相应的lua测试代码:
-- The following call will fail with the above error
MyResourceManager:loadResource("abc.res")
--MyResourceManager:loadResource("xyz.res")
-- If the function is commented out, this will …Run Code Online (Sandbox Code Playgroud)