0

I have this problem: A class of mine has a function called f. Some member function needs to call a global function called f. Now that's impossible, as I understand. To reach the global function from the class I need to call it under another name. How can I assign another name to it?

Do synonyms exist in C++ ? To define a preprocessor symbol doesn't work here: The names must be different AFTER preprocessing. So I have this global function:

double f(double x)
{
    return x;
}

I could of course write another copy of the function with another name:

double f_(double x)
{
    return x;
}

but that's ugly. An elegant solution, please. I'm thinking about defining a global function pointer:

double (*f_)(double x) = &f;

or an inline wrapper function:

inline double f_(double x)
{
    return f(x);
}

But these are not truly synonyms. Is there a nicer way?

Henrik4
  • 464
  • 3
  • 13

1 Answers1

3

I have this problem: A class of mine has a function called f. Some member function needs to call a global function called f. Now that's impossible, as I understand.

It's not impossible. Simply use the fully qualified name ::f to refer to the hidden global function.

As for giving an alias for a function, there is no way for the function itself, but if you put the function in a namespace, then you can give an alias to the namespace and thereby an alias to the qualified name of the function.

eerorika
  • 232,697
  • 12
  • 197
  • 326