【发布时间】:2012-03-12 11:13:01
【问题描述】:
通常,书籍通常不鼓励在包装 c 结构时使用转换运算符。例如,c++ 字符串(被认为是)是 C 字符数组的包装器,它不提供转换运算符。相反,它给出了方法c_str()。
但是,我认真地认为我的情况可能是个例外。我正在结束 SDL_Surface. 这是类声明。
/** Wraps up SDL_Surface **/
class surface
{
SDL_Surface* _surf;
public:
/** calls SDL_LockSurface().
** throws surface_lock_exception on failure.
**/
void lock();
/** calls SDL_UnlockSurface() **/
void unlock();
/** calls SDL_LoadBMP().
** throws image_load_exception on failure.
**/
void load_bmp(const string&);
/** calls SDL_FreeSurface(). **/
void free();
/** destructor. Also free()s the internal SDL_Surface. **/
~surface();
};
在这种情况下,我认真地认为我应该在 SDL_Surface* 中添加一个转换运算符,以便与其他需要 SDL_Surface* 的 SDL 函数兼容。
你怎么看:
- 转换操作员会谨慎吗?
- 还是应该使用
c_str()之类的方法? - 或者还有其他更好的解决方案吗?
【问题讨论】:
标签: c++ oop operator-keyword conversion-operator