C++:如何通过公共方法调用私有方法?

Tri*_*orn 1 c++ class function

对于我们的项目,我们给出了一个代码片段,我们不应以任何方式对其进行编辑。我们只允许为上述代码段中的原型编写函数定义。

我的问题和问题是关于当以这种方式编写代码时我应该如何调用私有函数:

class ClassOne {
    private:
    void methodOne();

    public:
    void methodTwo();
};
Run Code Online (Sandbox Code Playgroud)

所以我应该能够通过methodTwo访问methodOne,但不需要{ methodTwo();}在methodOne旁边写。请帮帮我?

Jar*_*ows 6

你已经有你的class

class ClassOne {
    private:
    void methodOne();

    public:
    void methodTwo();
};
Run Code Online (Sandbox Code Playgroud)

实现functions你的class

void ClassOne::methodOne() { // <-- private
   // other code
}

void ClassOne::methodTwo() { // <-- public
   // other code
   methodOne();              // <-- private function called here
}
Run Code Online (Sandbox Code Playgroud)