calling a global function with a class method with the same declaration

爱⌒轻易说出口 提交于 2019-12-17 23:34:09

问题


I would like to wrap a C library within a C++ class. For my C++ class I also would like to have the same declaration used by these C function: is it possible to do that?

If for example I have the case below how would it be possible to distinguish the C-function from the C++ one? I would like to call the C one off course.

 extern int my_foo( int val ); //

 class MyClass{
    public:
    int my_foo( int val ){
           // what to write here to use
           // the C functions?
           // If I call my_foo(val) it will call
           // the class function not the global one
    }
 }

回答1:


Use the scope resolution operator :::

int my_foo( int val ){
    // Call the global function 'my_foo'
    return ::my_foo(val);
}



回答2:


Use Qualified name lookup

::my_foo(val);

This tells the compiler you want to call the global function and not the local function.




回答3:


::my_foo(val);

that should do it.



来源:https://stackoverflow.com/questions/7149922/calling-a-global-function-with-a-class-method-with-the-same-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!