Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

gcc

word-wrap

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     }  } 
like image 968
Abruzzo Forte e Gentile Avatar asked Aug 22 '11 15:08

Abruzzo Forte e Gentile


People also ask

How do you call a global function?

::my_foo(val); This tells the compiler you want to call the global function and not the local function.

How do you call a function in the same class?

There are two ways to call another method function from the same class. First, you can use a dot/period to access the method from the class variable. Second, you can simply call the function and pass the class object as an argument.

Can you call a function outside the class?

Whenever the definition of a class member appears outside of the class declaration, the member name must be qualified by the class name using the :: (scope resolution) operator. The following example defines a member function outside of its class declaration.


1 Answers

Use the scope resolution operator :::

int my_foo( int val ){     // Call the global function 'my_foo'     return ::my_foo(val); } 
like image 174
Adam Rosenfield Avatar answered Oct 05 '22 12:10

Adam Rosenfield