1

Let's say I have the following program:

template <class X> void foo(X val) { }

template <> void foo<double>(double val) { }

class obj
{
public:
   void callFoo() { foo(7.3); }
private:
   void foo(int val) { }
};

int main()
{
    obj o;
    o.callFoo();
}

Why the foo(int val) is chosen? Doesn't it supposed to be the foo<double>(double val)? I thought maybe the class scope caused the foo(int val) to be called.

Cœur
  • 37,241
  • 25
  • 195
  • 267
maor levi
  • 39
  • 1
  • By the way, this is *not* overloading, as you have the question tagged. Overloading would be if you had defined a second `foo` function as a member of the `obj`, but taking a different parameter (e.g., a `double` instead of an `int`). The code you've shown here is actually *hiding* or *shadowing* the functions. – Cody Gray - on strike Jan 10 '16 at 14:29

0 Answers0