Half Life2代码中比较垃圾的地方

cproom 2003-10-15 04:21:43


class CTexture : public ITextureInternal
{
.
.
.
}

ITextureInternal *ITextureInternal::CreateFileTexture( const char *pFileName, bool bNormalMap )
{
CTexture *pTex = new CTexture;
pTex->InitFileTexture( pFileName );
return pTex;
}

基类的函数中分配一个派生类对象,并且调用了派生类的函数,并且此函数并非由基类继承而来。个人认为这是非常不好的设计,想听听大家的看法。

...全文
73 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nownow 2003-10-20
  • 打赏
  • 举报
回复
嘿嘿,这就是COM的类工厂模型嘛,用得多了去了,不用大惊小怪!~
messagebox 2003-10-20
  • 打赏
  • 举报
回复
不要随便下结论,浮躁
Icykorpio 2003-10-18
  • 打赏
  • 举报
回复
这不是个有问题的写法,呵呵,renxiaoyu说得一点没错的
Icykorpio 2003-10-18
  • 打赏
  • 举报
回复
这种写法很常见啊,前面那个create类函数一般需要是静态函数,有时候也写成一个全局函数,那个bool没用的话就是预留给未来扩展的,不然也无所谓用工厂。简言之,就是把常见的过程化工厂(全局create)移入接口里面作为静态,这样就显得更OO一些而已。
bluenet21 2003-10-18
  • 打赏
  • 举报
回复
楼主,没必要这么匆忙得下结论,
不要将自己从来没用过的程序设计方法称之为垃圾!

我倒觉得这种方法很好,用户只管接口怎么用,具体实现不用管,这样有便于扩展,有点像COM.
warmchang 2003-10-18
  • 打赏
  • 举报
回复
嗯,楼主设计模式没有学好,^_^
ttmmdd 2003-10-17
  • 打赏
  • 举报
回复
HL2不是模式的教学代码,也不要太把一个有问题的写法当回事了.
renxiaoyu(一条直线,一个目标) 的解释过于牵强.
renxiaoyu 2003-10-17
  • 打赏
  • 举报
回复
想详细了解的,请参考<<JAVA与模式>>第12章:简单工厂(Simple Factory)模式
renxiaoyu 2003-10-17
  • 打赏
  • 举报
回复
设计模式提供的是一种思维方式,并没有严格的格式规定。

这段代码就用的是简单工厂模式,包括工厂、抽象产品、具体产品三个角色。

通常情况,工厂、抽象产品、具体产品分别是三个类。

HL2的这段代码,工厂角色和抽象产品角色合并,一个抽象产品类同时是子类的工厂,这很合理。

还有一种情况,工厂、抽象产品、具体产品合并成一个类,自己创建自己。
弟十六 2003-10-16
  • 打赏
  • 举报
回复
不能就事论事,要从整体观察。
Hackevin 2003-10-16
  • 打赏
  • 举报
回复
i++的效率比++i差
mengxihe 2003-10-16
  • 打赏
  • 举报
回复
for ( i = 0; i < 6; ++i )
看着就不是很爽,为什么不i++呢?
acceptspf 2003-10-16
  • 打赏
  • 举报
回复
关注 AND UP
ttmmdd 2003-10-16
  • 打赏
  • 举报
回复
Builder pattern 也不是这样个用法的.
Builder 可不会生成一个Builder 的派生类.

JohnFractal 2003-10-16
  • 打赏
  • 举报
回复
的确怪怪的

本来还以为是一个工厂,但是工厂居然生产工厂的子类……


可能是懒得写一个工厂类,结果将工厂和产品写在同一个类里面了……

严重FT!
renxiaoyu 2003-10-16
  • 打赏
  • 举报
回复
Q: Someone told me that if I want to increment a variable and that's all, I should use ++x instead of x++. Is this true?


A: If incrementing is all that you want to do (you are not assigning the variable to something else), then yes this is a good practice, especially if you are using the increment operator on a class.



code:--------------------------------------------------------------------------------// Prefix operators are preferred in cases in the following common cases:

for (;checkstate(x);++x) dosomething(x);

++x;--------------------------------------------------------------------------------

For the standard data types there is usually no performance difference, but for classes there are! The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference. The following code exemplifies this:


code:--------------------------------------------------------------------------------
class MyClass
{
public:
MyClass& operator++() //Prefix increment operator (++x)
{
//Perform increment operation
return *this;
}

MyClass operator++(int) //Postfix increment operator (x++)
{
MyClass temp = *this;
//Perform increment operation
return temp;
}
};

renxiaoyu 2003-10-16
  • 打赏
  • 举报
回复
同意akun(疑无路)兄的看法,ITextureInternal中的“I”就是Interface(接口)的意思。
Challenger17 2003-10-16
  • 打赏
  • 举报
回复
C++ Primer中Lippman都是用++i
renxiaoyu 2003-10-16
  • 打赏
  • 举报
回复
to shilong(银羽·以吻赠剑):
在语句:for ( i = 0; i < 6; ++i )里i++的效率比++i差。
在语句:i++;++i;里i++和++i效率一样。

前者多了一个中间变量。
shilong 2003-10-16
  • 打赏
  • 举报
回复

i++的效率比++i差??????????????
====================================
256: i++;
00406274 mov eax,dword ptr [ebp-8]
00406277 add eax,1
0040627A mov dword ptr [ebp-8],eax
257: ++i;
0040627D mov eax,dword ptr [ebp-8]
00406280 add eax,1
00406283 mov dword ptr [ebp-8],eax
加载更多回复(5)

8,301

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧