Calling a global function with the same name as a member function

A

anoopsaha

Hi all,
I have a class X with a member function dump(). Now there is another
version of dump() which is globally defined in some third party include
file. From another member function of X, I wish to call the global
dump() function. How can I achieve that? Because of legacy reasons, I
cannot change the name of either of the functions, or their number of
arguments.

Here is a piece of code to illustrate the problem.

#include <iostream>
using namespace std;

int dump(int input, int in2)
{
cout << " Global dump" << endl;
}

class A
{
public:
A() {};
~A() {};

int dump(int input)
{
cout << "In class A" << endl;
}
int func(bool in)
{
if (in)
dump (0);
else
dump (1, 1);
}
};

when you compile it, it gives the following error ;
anoop@anoop[27] g++ -c a.cxx
a.cxx: In member function `int A::func(bool)':
a.cxx:24: no matching function for call to `A::dump(int, int)'
a.cxx:16: candidates are: int A::dump(int)

Thanks in advance,
Anoop
 
S

Sharad Kala

Hi all,
I have a class X with a member function dump(). Now there is another
version of dump() which is globally defined in some third party include
file. From another member function of X, I wish to call the global
dump() function. How can I achieve that? Because of legacy reasons, I
cannot change the name of either of the functions, or their number of
arguments.

Here is a piece of code to illustrate the problem.

#include <iostream>
using namespace std;

int dump(int input, int in2)
{
cout << " Global dump" << endl;

Return value ??
}

class A
{
public:
A() {};
~A() {};

int dump(int input)
{
cout << "In class A" << endl;

Return value ??
}
int func(bool in)
{
if (in)
dump (0);
else
dump (1, 1);

::dump(1, 1);

Sharad
 
K

Karl Heinz Buchegger

Hi all,
I have a class X with a member function dump(). Now there is another
version of dump() which is globally defined in some third party include
file. From another member function of X, I wish to call the global
dump() function. How can I achieve that? Because of legacy reasons, I
cannot change the name of either of the functions, or their number of
arguments.

Specifiy that the function is to be looked up in the global namespace
int func(bool in)
{
if (in)
dump (0);
else
dump (1, 1);

::dump( 1, 1 );
 
S

Shan

From another member function of X, I wish to call the global
dump() function. How can I achieve that?

Use scope resolution operator :: for accessing the global third party
function, like :

::dump();

Cheers
Shan
 
B

Bob Hairgrove

Hi all,
I have a class X with a member function dump(). Now there is another
version of dump() which is globally defined in some third party include
file. From another member function of X, I wish to call the global
dump() function. How can I achieve that? Because of legacy reasons, I
cannot change the name of either of the functions, or their number of
arguments.
..
[snip]

(1) You can rename the member function to something else;
(2) You can qualify the name (i.e. write ::dump(...)).
 
A

anoopsaha

Thanks all.

::dump() is what I was looking for. Should have known it. It works
fine now


Anoop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top