私はCinderフレームワークを学んでいます。 あり、この枠組みの中でクラスTexture
があり、それは次のように使用することができます:boolへのこの変換はどのように機能しますか?
Texture myImage;
myImage.loadImage(/*...*/);
if(myImage)
{
// draw the image.
}
myImage
が対象ですので、私は、このことについて混乱しました。それを条件として使うことは私には意味がありません。私はmyImage.exist();
のようなものを期待していました。だから私は、コードを通じて段階、およびそれがTexture
クラスが定義された変換演算子を持っていることが判明:Objには次のように定義されて
public:
//@{
//! Emulates shared_ptr-like behavior
typedef std::shared_ptr<Obj> Texture::*unspecified_bool_type;
// What is this???
operator unspecified_bool_type() const { return (mObj.get() == 0) ? 0 : &Texture::mObj; }
void reset() { mObj.reset(); }
//@}
:
protected:
struct Obj {
Obj() : mWidth(-1), mHeight(-1), mCleanWidth(-1), mCleanHeight(-1), mInternalFormat(-1), mTextureID(0), mFlipped(false), mDeallocatorFunc(0) {}
Obj(int aWidth, int aHeight) : mInternalFormat(-1), mWidth(aWidth), mHeight(aHeight), mCleanWidth(aWidth), mCleanHeight(aHeight), mFlipped(false), mTextureID(0), mDeallocatorFunc(0) {}
~Obj();
mutable GLint mWidth, mHeight, mCleanWidth, mCleanHeight;
float mMaxU, mMaxV;
mutable GLint mInternalFormat;
GLenum mTarget;
GLuint mTextureID;
bool mDoNotDispose;
bool mFlipped;
void (*mDeallocatorFunc)(void *refcon);
void *mDeallocatorRefcon;
};
std::shared_ptr<Obj> mObj;
私はoperator int() const
がimplictly int型にオブジェクトを変更することができることを知っていますしかし、unspecified_bool_typeはどのように機能していますか? if(myImage)
が実行されている場合、デバッガはoperator unspecified_bool_type() const { return (mObj.get() == 0) ? 0 : &Texture::mObj; }
で停止します。
そして、私はここで文法について少し混同されることが、
typedef std::shared_ptr<Obj> Texture::*unspecified_bool_type;
が何を意味するのでしょうか?
そして
OBJの中void (*mDeallocatorFunc)(void *refcon);
はmDeallocatorFuncは、クラスのObj、プロトタイプを持つ関数への関数ポインタのメンバーであることを意味している:void xxx(void *)
?
"安全なブールイディオム"の呪文のようです... –
@KerrekSB私はそれを持っていると思います。それは 'Texture'を' shared_ptr 'に変換できる変換演算子を定義し、残りのチェックが提供されます'shared_ptr 'によって。しかし、 'typedef std :: shared_ptrは何ですか? Texture :: * unspecified_bool_type;' mean ?? 'typedef std :: shared_ptr unspecified_bool_type;'との違いはありますか?私は文法のこの種、特に ':: *'とここで混乱しています。どうもありがとうございました。 –
shengy
そのことは特に(普通のポインタではない!)メンバーへのポインタですが、それ以上のコードは読めません。 SBIを実装する多くの一般的な方法がありますので、少し検索するとコードによく似た説明が見つかるかもしれません。 –