顯示具有 OOP 標籤的文章。 顯示所有文章
顯示具有 OOP 標籤的文章。 顯示所有文章

2014年3月7日 星期五

C++中 public,protected, private 访问标号小结

C++中 public,protected, private 访问标号小结
http://cnmtjp.blog.51cto.com/204390/36548/

第一:private, public, protected 访问标号的访问范围。
private:只能由1.该类中的函数、2.其友元函数访问。
不能被任何其他访问,该类的对象也不能访问。

protected:可以被1.该类中的函数、2.子类的函数、以及3.其友元函数访问。
但不能被该类的对象访问。

public:可以被1.该类中的函数、2.子类的函数、3.其友元函数访问,也可以由4.该类的对象访问。

注:友元函数包括3种:设为友元的普通的非成员函数;设为友元的其他类的成员函数;设为友元类中的所有成员函数。

第二:类的继承后方法属性变化。
private 属性不能够被继承。
使用private继承,父类的protected和public属性在子类中变为private;
使用protected继承,父类的protected和public属性在子类中变为protected;
使用public继承,父类中的protected和public属性不发生改变;

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;
}