J
joseph cook
I'm confused by this output. I would expect the more specific
overloaded function to be selected:
#include <iostream>
class Base
{
};
class Derived : public Base
{};
class Foo
{
public:
template<typename U>
void bar(U* in, int a)
{
std::cout<<"Generic Call"<<std::endl;
}
void bar(Base* in, int a)
{
std::cout<<"A more appropriate choice"<<std::endl;
}
};
int main()
{
Derived *d = new Derived;
Foo foo;
foo.bar(d,1,1) // calling with derived
}
This prints "Generic Call". Is this correct? Why? I would have
expected the compiler to either pick the more specific overload, or
complain that the call was ambiguous.
What's going on ?
What's the fix ?
Thanks in advance,
overloaded function to be selected:
#include <iostream>
class Base
{
};
class Derived : public Base
{};
class Foo
{
public:
template<typename U>
void bar(U* in, int a)
{
std::cout<<"Generic Call"<<std::endl;
}
void bar(Base* in, int a)
{
std::cout<<"A more appropriate choice"<<std::endl;
}
};
int main()
{
Derived *d = new Derived;
Foo foo;
foo.bar(d,1,1) // calling with derived
}
This prints "Generic Call". Is this correct? Why? I would have
expected the compiler to either pick the more specific overload, or
complain that the call was ambiguous.
What's going on ?
What's the fix ?
Thanks in advance,