2011年9月17日 星期六

關於C++ friend (沒看過這樣用template)

http://mtlung.blogspot.com/2008/06/inlcude-int-main-stdcout-hello-worldn.html

沒看過這樣用template


// Texture.h
class Texture { 
public
 friend class IResourceLoader; 
 uint width() const { return mWidth; } 
 uint height() const { return mHeight; } 
 
 // We declare a templated inner class here (but not defined yet)
 template<class> class PrivateAccessor; //看不懂@@
 
 private
 uint mWidth; 
 uint mHeight; 
};
 
class IResourceLoader {}; 
 
// JpegLoader.cpp
// Define Texture::PrivateAccessor here, access what ever you want
template<> //看不懂@@
class Texture::PrivateAccessor<jpegloader> {
public:
 static size_t& width(Texture& texture) {
  return texture.mWidth;
 }
 static size_t& height(Texture& texture) {
  return texture.mHeight;
 }
};
typedef Texture::PrivateAccessor<jpegloader> Accessor;
 
void JpegLoader::load() {
 // ... 
 Accessor::width(mTexture) = 128; // Ok! no problem
 Accessor::height(mTexture) = 128;
}

沒有留言:

張貼留言