1

I would like to call the unscoped function "bar" from "somelib" within the "bar" method of Foo.

// .h
class Foo
{
    int bar();
};

// .cpp
#include "Foo.h"
#include <somelib> // contains unscooped function bar()

int Foo::bar()
{
    return bar(); // unwanted recursive function
}

One way to solve it is to make use of a helper function, such as "bar_helper"

// .cpp
#include "Foo.h"
#include <somelib> // contains unscooped function bar()

// unnamed namespace
namespace
{
    int bar_helper()
    {
        return bar(a);
    }
}

int Foo::bar()
{
    return bar_helper();
}

  • Can it be made more pretty?
  • Is there a better solution?

2 Answers 2

2

If the non-member bar function is in the global scope, you can use the scoping operator :::

int Foo::bar()
{
    return ::bar();
}
Sign up to request clarification or add additional context in comments.

Comments

2

Be explicit about the scope, in this case, the global one:

return ::bar();
       ^^

1 Comment

Thanks for the help! I had to choose Joachim's answer since I liked his explanation a tiny bit better, however I have to commend you for answering a few seconds earlier than him.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.