【问题标题】:calling a global function with a class method with the same declaration使用具有相同声明的类方法调用全局函数
【发布时间】:2011-11-01 06:30:10
【问题描述】:

我想在 C++ 类中封装一个 C 库。对于我的 C++ 类,我也希望这些 C 函数使用相同的声明:可以这样做吗?

例如,如果我有以下情况,如何区分 C 函数和 C++ 函数?我想把 C 称为偏离路线。

 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
    }
 }

【问题讨论】:

    标签: c++ gcc word-wrap


    【解决方案1】:

    使用scope resolution operator ::

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

    【讨论】:

      【解决方案2】:

      使用限定名查找

      ::my_foo(val);
      

      这告诉编译器你要调用全局函数而不是局部函数。

      【讨论】:

        【解决方案3】:
        ::my_foo(val);
        

        应该这样做。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-08
          • 2020-01-22
          • 2011-07-04
          • 1970-01-01
          • 2019-03-22
          • 1970-01-01
          相关资源
          最近更新 更多