Jor*_*rdi 2 c++ inheritance masking
假设我有这个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?
}
};
Run Code Online (Sandbox Code Playgroud)
我必须从Parent类继承,以便在框架中自定义一些功能.但是,Parent该类正在屏蔽exampleFunction我想要调用的全局.有什么方法可以叫它myFunction吗?
(我实际上有这个问题,如果这有任何区别调用库中的time函数<ctime>)
GMa*_*ckG 17
请执行下列操作:
::exampleFunction()
Run Code Online (Sandbox Code Playgroud)
:: 将访问全局命名空间.
如果您#include <ctime>,您应该能够在命名空间中访问它std:
std::time(0);
Run Code Online (Sandbox Code Playgroud)
要避免这些问题,请将所有内容放在名称空间中,并避免使用全局using namespace指令.