嵌套类可以访问外围类的私有成员
来源:互联网 发布:淘宝女装春季服饰图片 编辑:程序博客网 时间:2024/04/26 11:21
看c++primer时读到一句话 ——
p800页关于nested class的,
listitem is not permitted to access a private data member of its enclosing class, such as list.
意思是说,内部类不能访问外部类的私有成员,但我自己做了实验,内部类是可以访问外部类的私有成员的.
最后翻看C++11标准,才知道问题的原因在于新版 C++ 标准对此有了不同的规定,具体见:
c++03 11.8/1
The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual
access rules (clause 11) shall be obeyed.
c++11 11.7/1
A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed.
p800页关于nested class的,
listitem is not permitted to access a private data member of its enclosing class, such as list.
意思是说,内部类不能访问外部类的私有成员,但我自己做了实验,内部类是可以访问外部类的私有成员的.
最后翻看C++11标准,才知道问题的原因在于新版 C++ 标准对此有了不同的规定,具体见:
c++03 11.8/1
The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual
access rules (clause 11) shall be obeyed.
c++11 11.7/1
A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed.
根据 c++11 11.7/1,我的代码是合法的。根据 c++03 11.8/1 和 c++ primer 成书的年代,Lippman 也是正确的。
代码:
#include <iostream>using namespace std;template<typename T>class A{public:A(const T& val):m_val(val){}private:class B{public:void print(const A& obj);};public:B* Get();private:B test;T m_val;};template<typename T>typename A<T>::B* A<T>::Get(){return &test;}template<typename T>void A<T>::B::print(const A& obj){cout<<obj.m_val<<endl;}int main(){A<int>obj(2);obj.Get()->print(obj);return 0;}
#include <iostream>using namespace std;template<typename T>class A{public:A(const T& val);~A();private:class B;public:B* Get();private:B* m_pTest;T m_val;}; template<typename T>class A<T>::B{public:void print(const A& obj);}; template<typename T>A<T>::A(const T& val):m_pTest(new B),m_val(val){}template<typename T>A<T>::~A(){delete m_pTest; m_pTest=NULL;}template<typename T>typename A<T>::B* A<T>::Get(){return m_pTest;}template<typename T>void A<T>::B::print(const A& obj){cout<<obj.m_val<<endl;}int main(){A<int>obj(2);obj.Get()->print(obj);return 0;}
- 嵌套类可以访问外围类的私有成员
- C++ 嵌套类与外围类间的成员访问
- C++ 嵌套类与外围类以及局部类对成员变量的访问 【数据结构】
- C++嵌套类及对外围类成员变量的访问
- java内部类----安全问题-----访问外围类的私有变量
- 外部类可以访问内部类的私有成员
- 访问类的私有成员
- 访问类的私有成员
- 学习笔记-----关于C++中类的成员函数可以访问私有成员的问题
- 访问私有类成员
- C++本类可以访问本身类的私有成员(即拷贝构造函数可以访问本身对象的私有成员)
- 嵌套类 外围类
- 类的私有数据成员的访问
- 用#define访问类的私有成员
- 利用反射访问类的私有成员
- c#访问类的私有成员
- 指针访问类的私有成员
- 访问类中的私有成员的
- <Linux+Qt>Linux+Qt学习(二)qt编程相关网站
- Drupal 中文安装教程
- DNS工作原理及其过程
- hdu 1869
- Windows静态库和动态库的创建和使用
- 嵌套类可以访问外围类的私有成员
- C++打开一个URL
- Java学习之道:问题一
- lucene概述
- Keil-MDK的 Error: L6630E: Invalid token start expected number or ( but found。。。
- 学习java的一些笔记(7)
- Unity 3D 学习笔记
- linux ar 打包库到另一个库中
- 1M的宽带的下载速度有时还不足100K?