gpt4 book ai didi

c++ - 在 C++ 中为成员对象调用 protected 方法

转载 作者:太空宇宙 更新时间:2023-11-04 15:21:04 25 4
gpt4 key购买 nike

如果我有两个类,例如像这样

class A {
...
protected:
B* test;
aFunction();
};

class B {
...
protected:
A* test1;

public:
bFunction();
};

我可以在 B 类的 bFunction() 中执行此操作吗:

bFunction(){
test1->aFunction();
}

基本上,我可以从不是从该函数派生的类调用某个类的 protected 函数吗?

最佳答案

protected 的“要点”是只有从基类派生的类才能调用这些函数。

如果你有充分的理由这样做,那么让类(class)成为 friend ,例如在 class A 中添加 friend class B;

关于c++ - 在 C++ 中为成员对象调用 protected 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19080753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com