Function overloading — two functions only differ by a default parameter

前端 未结 7 2094
有刺的猬
有刺的猬 2020-12-06 07:21
class A{
    public:
        void foo(int x)
        {
            cout << \"foo with one\\n\";
        }

        void foo(int x, int y=10)
        {
                 


        
7条回答
  •  醉梦人生
    2020-12-06 07:54

    No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either.

    You can only overload functions only on the basis of:

    • Type of arguments
    • Number of arguments
    • Sequence of arguments &
    • Qualifiers like const and volatile.

    Ofcourse, Whether the overload is accepted by the compiler depends on the fact:
    If the compiler resolve calling of the function unambiguously.

    In your case, the compiler cannot resolve the ambiguity, for ex: The compiler wouldn't know which overloaded function to call if you simple called the function as:

     foo(100);
    

    The compiler cannot make the decision and hence the error.

提交回复
热议问题