【问题标题】:How can I call a masked function in C++?如何在 C++ 中调用屏蔽函数?
【发布时间】:2010-12-08 06:31:36
【问题描述】:

假设我有这个 C++ 代码:

void exampleFunction () { // #1
    cout << "The function I want to call." << endl;
}

class ExampleParent { // I have no control over this class
public:
    void exampleFunction () { // #2
        cout << "The function I do NOT want to call." << endl;
    }
    // other stuff
};

class ExampleChild : public ExampleParent {
public:
    void myFunction () {
        exampleFunction(); // how to get #1?
    }
};

我必须从Parent 类继承才能自定义框架中的某些功能。但是,Parent 类掩盖了我想调用的全局exampleFunction。有什么方法可以通过myFunction 调用它吗?

(如果这有什么不同的话,我实际上在调用&lt;ctime&gt; 库中的time 函数时遇到了这个问题)

【问题讨论】:

    标签: c++ inheritance masking


    【解决方案1】:

    执行以下操作:

    ::exampleFunction()
    

    :: 将访问全局命名空间。

    如果你#include &lt;ctime&gt;,你应该可以在命名空间std中访问它:

    std::time(0);
    

    为避免这些问题,请将所有内容放在命名空间中,并避免使用全局 using namespace 指令。

    【讨论】:

    • ::std::time(0) 当然,可以毫无歧义地访问 std 版本
    猜你喜欢
    • 2019-06-19
    • 2010-12-17
    • 2021-11-21
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多