⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 模板友员访私有.cpp

📁 C++源代码集
💻 CPP
字号:
#include <iostream>
using namespace std;

void test(); //main()不能作为模版类的友元,所以...

template<class T>
class base  {
friend void test();
public:
T* pub;
T* pri;
private:
void fx(void);
};
template<class T>
void base<T>::fx(void) {
this->pub=static_cast<T*>(this);
this->pri=static_cast<T*>(this);

this->pub->pubfx();//ok
this->pri->prifx();//no,can not access a member
}

class dev : public base<dev> {
friend class base<dev>;
public:
void pubfx(void);
private:
void prifx(void);
};

void dev :: pubfx(void)
{
cout<<"public"<<endl;
}

void dev::prifx(void)
{
cout<<"private"<<endl;
}

void main(){
test(); 
}

void test() {
base<dev>* p = new dev;
p->fx();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -