Skip to main content
Engineering LibreTexts

14.2.2: Function overloading and const keyword

  • Page ID
    49970
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Overload and  const

    #include<iostream>
    using namespace std;
      
    class Test
    {
       protected:
         int num1;
       public:
         Test (int i):num1(i) { }
         void fun() const
         {
             cout << "fun() const called " << endl;
         }
         void fun()
         {
             cout << "fun() called " << endl;
         }
    };
      
    int main()
    {
        Test t1 (10);
        const Test t2 (20);
        t1.fun();
        t2.fun();
        return 0;
    }
    

    Output: The above program compiles and runs fine, and produces following output.

    fun() called
    fun() const called

    The two methods ‘void fun() const’ and ‘void fun()’ have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, ‘const void fun()’ is called on const object and ‘void fun()’ is called on non-const object.

    C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer.

    What about parameters?

    Rules related to const parameters are interesting. Let us first take a look at following two examples. The program 1 fails in compilation, but program 2 compiles and runs fine.

    // PROGRAM 1 (Fails in compilation)
    #include<iostream>
    using namespace std;
      
    void fun(const int Num1)
    {
        cout << "fun(const int) called " << Num1;
    }
    void fun(int Num1)
    {
        cout << "fun(int ) called " << Num1;
    }
    int main()
    {
        const int myNum = 10;
        fun(myNum);
        return 0;
    }
    

     Output is:

    error: redefinition of ‘void fun(int)’
    void fun(int Num2)
         ^~~

    Then the second piece of code:

    // PROGRAM 1 (Fails in compilation)
    #include<iostream>
    using namespace std;
      
    void fun(const int i)
    {
        cout << "fun(const int) called ";
    }
    void fun(int i)
    {
        cout << "fun(int ) called " ;
    }
    int main()
    {
        const int i = 10;
        fun(i);
        return 0;
    }
    

    which produces the outpu: 

    const fun() GeeksforGeeks
    

     C++ allows functions to be overloaded on the basis of const-ness of parameters only if the const parameter is a reference or a pointer. That is why the program 1 failed in compilation, but the program 2 worked fine. This rule actually makes sense. In program 1, the parameter ‘myNum’ is passed by value, so ‘Num1’ in fun() is a copy of ‘myNum’ in main(). Hence fun() cannot modify ‘myNum’ of main(). Therefore, it doesn’t matter whether ‘Num1’ is received as a const parameter or normal parameter. When we pass by reference or pointer, we can modify the value referred or pointed, so we can have two versions of a function, one which can modify the referred or pointed value, other which can not.

    Adaprted from:
    "Function overloading and const keyword" by GeeksforGeeks, Geeks for Geeks is licensed under CC BY-SA 4.0


    14.2.2: Function overloading and const keyword is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?