Two functions with one name one in this one in its friend

Which of two functions of one name will be called, the one declared in a class of the object invoking it or one in a class within which a friend to the first class is stated?
Last edited on
Why not try it out yourself?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Example program
#include <iostream>

void func()
{
    std::cout << "func [outside class]\n";
}

class Floob {
    friend void func();
    
  public:
    void floo()
    {
        func();   
    }
    
    void func()
    {
        std::cout << "func [inside class]\n";   
    }
  private:
    int a;
};

int main()
{
    Floob floob;
    floob.floo();
}


If you want to access the friend function, you can do:
1
2
3
4
    void floo()
    {
        ::func();   
    }
Last edited on
Topic archived. No new replies allowed.