对于我们的项目,我们得到了一段代码片段,我们不应以任何方式对其进行编辑。我们只允许为上述代码段中的原型(prototype)编写函数定义。
我的问题和问题是关于当代码以这种方式编写时我应该如何调用私有(private)函数:
class ClassOne {
private:
void methodOne();
public:
void methodTwo();
};
因此我应该能够通过 methodTwo 访问 methodOne,但无需在 methodOne 旁边编写 { methodTwo();}
。请帮帮我好吗?
最佳答案
你已经有了你的类
:
class ClassOne {
private:
void methodOne();
public:
void methodTwo();
};
实现类
的功能
:
void ClassOne::methodOne() { // <-- private
// other code
}
void ClassOne::methodTwo() { // <-- public
// other code
methodOne(); // <-- private function called here
}
关于C++ : How do I call private methods through public ones?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639374/