C++ : How do I call private methods through public ones?

标签 c++ function class

对于我们的项目,我们得到了一段代码片段,我们不应以任何方式对其进行编辑。我们只允许为上述代码段中的原型(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/

相关文章:

c++ - 为什么 O_CREAT 创建只能由管理员访问的文件?

c++ - C++继承中成员函数的访问

c++ - 返回使用自定义分配器的 vector

c++ - 返回函数参数,可能,糟糕的风格?

.NET 插件安全

c++ - 构建 CMake 时使用非默认 libstdc++?

Python函数的参数默认值是从另一个模块导入的

function - 如何将 Derby 数据库与 Using Join 和 aggregate 函数一起使用?

python - 类类名: AND class classname(): AND class classname(object):之间的区别

python - 用 Python 编写一次性或匿名类?