문제

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?
도움이 되었습니까?

해결책

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

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

다른 팁

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

return ::bar();
       ^^
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top