- 主题:嵌套类(成员类)能不能访问外部类的私有成员?
在Bjarne第三版的书中,1.3节中说明了:A C++ nested class does not have access to an object of the enclosing class.
然而在16.2.13中又写到:
A member class (often called a nested class) can refer to types and static members of its enclosing class. It can only refer to non-static members when it is given an object of the enclosing class to refer to. To avoid getting into the intricacies of binary trees, I use purely technical ‘‘f() and g()’’-style examples.
A nested class has access to members of its enclosing class, even to private members (just as a member function has), but has no notion of a current object of the enclosing class.
今天早上我先是被AI给绕晕了,接着我又把AI给绕晕了
```cpp
class Encloser {
private:
struct PrivateType {/* many code here */};
class Nester {
public:
inline const PrivateType& Show() { return member_; }
private:
PrivateType member_;
};
};
```
这段代码请各位评析一下是否可以?
--
FROM 36.163.208.*
你完全可以直接把外部类去掉,可见性效果一样
【 在 wanllow 的大作中提到: 】
: 在Bjarne第三版的书中,1.3节中说明了:A C++ nested class does not have access to an object of the enclosing class.
: 然而在16.2.13中又写到:
: A member class (often called a nested class) can refer to types and static members of its enclosing class. It can only refer to non-static members when it is given an object of the enclosing class to refer to. To avoid getting into the intricacies of bina
: ...................
--
FROM 111.48.114.*
嵌套类没有父类都this指针,所有需要用到父类的都不可以。你这代码显然不行。
嵌套类是父类都一个成员,所以父类的变量访问权限通通对嵌套类开放。
【 在 wanllow 的大作中提到: 】
: 在Bjarne第三版的书中,1.3节中说明了:A C++ nested class does not have access to an object of the enclosing class.
:
: 然而在16.2.13中又写到:
: ...................
--来自微微水木3.5.17
--
FROM 58.246.152.*
感觉嵌套类很少用,我一般都是在class内部定义一些struct而不是定义class
需要定义嵌套class的地方,我都是拆出来单独定义,放到同一个namespace里
感觉花精力在这上面不太值
--
修改:z16166 FROM 114.254.115.*
FROM 114.254.115.*
完全有道理,我感觉我搞错了方向
【 在 z16166 的大作中提到: 】
: 感觉嵌套类很少用,我一般都是在class内部定义一些struct而不是定义class
: 需要定义嵌套class的地方,我都是拆出来单独定义,放到同一个namespace里
: 感觉花精力在这上面不太值
: ...................
--
FROM 36.163.208.*
结论:
1. 嵌套类可以使用外部类中定义的私有类型,无须加命名域符;
2. 嵌套类中无法直接访问外部类的成员变量和成员函数,无论公有私有都不行。
用下面的代码测试过了,嵌套类(成员类)可以使用外部类的私有类型(type)但是不能访问外部类中的私有成员变量,也不能调用外部类中的成员函数。
/*!
* This test code shows nested class can use the private type of enclosing
* class, but cannot access the members and functions of enclosing class, no
* matter private and public
*/
#include <iostream>
class Encloser {
public:
Encloser(double x, double y, double k1 = 1.0, double k2 = 1.0)
: member_(Nester(x, y)), k1_(k1), k2_(k2) {}
double Calculate() { return (member_.Show().x_ + member_.Show().y_) * k1_ * k2_; }
double Add(double a, double b) { return a + b; }
double k2_ = 1.0;
private:
class PrivateClass {
public:
double x_;
double y_;
};
class Nester {
public:
Nester(double x, double y) : member_({x, y}) {}
inline const PrivateClass& Show() { return member_; }
// inline double ShowK1() { return k1_; }
// inline double showK2() { return k2_; }
double InnerCalculate() {
double result = 0.0;
// result = Add(member_.x_, member_.y_);
// result = Product(result, k1_);
// result = Product(result, k2_);
return result;
}
private:
PrivateClass member_;
};
double k1_ = 1.0;
Nester member_;
double Product(double a, double b) { return a * b; }
};
int main() {
Encloser enclose_object(2.0, 3.0, 4.0);
std::cout << "The result is: " << enclose_object.Calculate() << std::endl;
return 0;
}
【 在 foliver 的大作中提到: 】
: 嵌套类没有父类都this指针,所有需要用到父类的都不可以。你这代码显然不行。
: 嵌套类是父类都一个成员,所以父类的变量访问权限通通对嵌套类开放。
:
--
修改:wanllow FROM 36.163.208.*
FROM 36.163.208.*
嵌套类只要拿到外部类的实例,通过这个实例就可以无视权限随便访问。
【 在 wanllow 的大作中提到: 】
: 结论:
: 1. 嵌套类可以使用外部类中定义的私有类型,无须加命名域符;
: 2. 嵌套类中无法直接访问外部类的成员变量和成员函数,无论公有私有都不行。
: ...................
--来自微微水木3.5.17
--
FROM 58.246.152.*
他在Nester里 用父类的子类PrivateType 这个是纯C++语法的问题吧?和this指针没有关系吧?
我记得他这种情况用default template argument就可以
template< class A = PrivateType>
class Nester
{
A xxxx
}
【 在 foliver 的大作中提到: 】
: 嵌套类没有父类都this指针,所有需要用到父类的都不可以。你这代码显然不行。
: 嵌套类是父类都一个成员,所以父类的变量访问权限通通对嵌套类开放。
:
--
FROM 115.192.190.*
对,迭代器就是这么实现的,关键是怎么把this指针传进去的问题
【 在 foliver 的大作中提到: 】
: 嵌套类只要拿到外部类的实例,通过这个实例就可以无视权限随便访问。
--
FROM 36.163.208.*
按你的方法轻松搞定了,
我也不想用太花骚的招式,只要能解决问题就好,
然而新手经常会被AI误导,它们说这种情况必须用嵌套才够安全,不会泄漏。
我后来想了想,谁会没事来动我的代码呀,先搞定再说,泄漏了也不怪我
【 在 z16166 的大作中提到: 】
: 感觉嵌套类很少用,我一般都是在class内部定义一些struct而不是定义class
: 需要定义嵌套class的地方,我都是拆出来单独定义,放到同一个namespace里
: 感觉花精力在这上面不太值
: ...................
--
FROM 36.163.208.*