Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Why no compiler error when virtual function has different parameter type ?
Forum Updated to NodeBB v4.3 + New Features

Why no compiler error when virtual function has different parameter type ?

Scheduled Pinned Locked Moved Solved C++ Gurus
12 Posts 4 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JKSHJ JKSH

    @Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:

    not able to find error with parameter type ?

    The compiler can't tell if you want to override or overload.

    C++11 has a new feature to detect these errors: You should always add the "override" keyword when you want to override a virtual function.

    class Base {
    public:
        virtual void func(int x);
    };
    
    class DerivedA : public Base {
    public:
        virtual void func(float x); // No error: This is a new overload with `float` arg
    };
    
    class DerivedB : public Base {
    public:
        void func(float x) override; // ERROR: This is not a valid override
    };
    
    class DerivedC : public Base {
    public:
        void func(int x) override; // No error: This is a valid override
    };
    
    V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by Vinoth Rajendran4
    #3

    @JKSH : Thanks for quick reply.

    With virtual keyword present in function, its obvious that we are expecting function override. Then why compiler fails to detect the error without we using "override" keyword ?

    JKSHJ 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      @JKSH : Thanks for quick reply.

      With virtual keyword present in function, its obvious that we are expecting function override. Then why compiler fails to detect the error without we using "override" keyword ?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #4

      @Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:

      With virtual keyword present in function, its obvious that we are expecting function override.

      No, it is not obvious -- it is ambiguous. "virtual" can mean "override an existing function" or "create a new virtual function".

      "override" makes it obvious that we are expecting a function override. Just use it, and you don't have to worry about this issue anymore.

      why compiler fails to detect the error without we using "override" keyword ?

      Because it is a logical error, not a compilation error. Compilers don't detect logical errors.

      Let me ask you: Why does the compiler fail to detect the following error?:

      int *x = nullptr;
      *x = 42;
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      V 1 Reply Last reply
      7
      • JKSHJ JKSH

        @Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:

        With virtual keyword present in function, its obvious that we are expecting function override.

        No, it is not obvious -- it is ambiguous. "virtual" can mean "override an existing function" or "create a new virtual function".

        "override" makes it obvious that we are expecting a function override. Just use it, and you don't have to worry about this issue anymore.

        why compiler fails to detect the error without we using "override" keyword ?

        Because it is a logical error, not a compilation error. Compilers don't detect logical errors.

        Let me ask you: Why does the compiler fail to detect the following error?:

        int *x = nullptr;
        *x = 42;
        
        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on last edited by
        #5

        @JKSH

        int *x = nullptr;
        *x = 42;
        

        This is logical error. compiler can't find the issue here.

        But I got curious to ask this question in forum because,

        class Base
        {
        public:
        	virtual void fun()
        	{
        		cout << "Base \n";
        	}
        };
        
        class Derived : public Base
        {
        public:
        	bool fun()  // compiler error in this case , return type different
        	{
        		cout << "Derived \n";
        	}
        };
        

        whereas no error here,

        class Base
        {
        public:
        	virtual void fun(int)
        	{
        		cout << "Base \n";
        	}
        };
        
        class Derived : public Base
        {
        public:
        	void fun(bool)  // no error, parameter type different 
        	{
        		cout << "Derived \n";
        	}
        };
        

        Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?

        jsulmJ JKSHJ 2 Replies Last reply
        1
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Because you can't overload a function when the return type is different.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          4
          • V Vinoth Rajendran4

            @JKSH

            int *x = nullptr;
            *x = 42;
            

            This is logical error. compiler can't find the issue here.

            But I got curious to ask this question in forum because,

            class Base
            {
            public:
            	virtual void fun()
            	{
            		cout << "Base \n";
            	}
            };
            
            class Derived : public Base
            {
            public:
            	bool fun()  // compiler error in this case , return type different
            	{
            		cout << "Derived \n";
            	}
            };
            

            whereas no error here,

            class Base
            {
            public:
            	virtual void fun(int)
            	{
            		cout << "Base \n";
            	}
            };
            
            class Derived : public Base
            {
            public:
            	void fun(bool)  // no error, parameter type different 
            	{
            		cout << "Derived \n";
            	}
            };
            

            Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @Vinoth-Rajendran4 You get an error with different return type because the signature of the virtual method is same, so compiler considers it to be the same method (return type is not part of the signature). Compiler simply could not decide reliably which version of a virtual method to call if the only difference would be its return type.
            But if you add a virtual method with same name but different parameter list then you are overloading that method, so why should compiler complain? It is perfectly fine to overload virtual methods like any other method.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            V 1 Reply Last reply
            7
            • V Vinoth Rajendran4

              @JKSH

              int *x = nullptr;
              *x = 42;
              

              This is logical error. compiler can't find the issue here.

              But I got curious to ask this question in forum because,

              class Base
              {
              public:
              	virtual void fun()
              	{
              		cout << "Base \n";
              	}
              };
              
              class Derived : public Base
              {
              public:
              	bool fun()  // compiler error in this case , return type different
              	{
              		cout << "Derived \n";
              	}
              };
              

              whereas no error here,

              class Base
              {
              public:
              	virtual void fun(int)
              	{
              		cout << "Base \n";
              	}
              };
              
              class Derived : public Base
              {
              public:
              	void fun(bool)  // no error, parameter type different 
              	{
              		cout << "Derived \n";
              	}
              };
              

              Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #8

              @Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:

              Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?

              @Christian-Ehrlicher and @jsulm have the correct answer.

              This also applies to non-virtual functions: You cannot have 2 functions with the same name and same parameters but different return types.

              
              // OK: Overloaded functions can have different return types
              void funA();
              bool funA(int);
              
              // Error: Not allowed to have same name and same parameters but different return types
              void funB();
              bool funB();
              

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              jsulmJ 1 Reply Last reply
              1
              • JKSHJ JKSH

                @Vinoth-Rajendran4 said in Why no compiler error when virtual function has different parameter type ?:

                Can you help me understand ,how compiler is able to detect return type mismatch in case virtual keyword is used ?

                @Christian-Ehrlicher and @jsulm have the correct answer.

                This also applies to non-virtual functions: You cannot have 2 functions with the same name and same parameters but different return types.

                
                // OK: Overloaded functions can have different return types
                void funA();
                bool funA(int);
                
                // Error: Not allowed to have same name and same parameters but different return types
                void funB();
                bool funB();
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #9

                @JKSH said in Why no compiler error when virtual function has different parameter type ?:

                You cannot have 2 functions with the same name and same parameters but different return types

                You can in two different classes in same class hierarchy as long as the method is not virtual. But in this case you're overriding anyway.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                JKSHJ 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @JKSH said in Why no compiler error when virtual function has different parameter type ?:

                  You cannot have 2 functions with the same name and same parameters but different return types

                  You can in two different classes in same class hierarchy as long as the method is not virtual. But in this case you're overriding anyway.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #10

                  @jsulm said in Why no compiler error when virtual function has different parameter type ?:

                  @JKSH said in Why no compiler error when virtual function has different parameter type ?:

                  You cannot have 2 functions with the same name and same parameters but different return types

                  You can in two different classes in same class hierarchy as long as the method is not virtual.

                  Ooh, good point

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Vinoth-Rajendran4 You get an error with different return type because the signature of the virtual method is same, so compiler considers it to be the same method (return type is not part of the signature). Compiler simply could not decide reliably which version of a virtual method to call if the only difference would be its return type.
                    But if you add a virtual method with same name but different parameter list then you are overloading that method, so why should compiler complain? It is perfectly fine to overload virtual methods like any other method.

                    V Offline
                    V Offline
                    Vinoth Rajendran4
                    wrote on last edited by
                    #11

                    @jsulm :

                    return type is not part of the signature
                    

                    Thanks for the info.

                    1 Reply Last reply
                    1
                    • V Offline
                      V Offline
                      Vinoth Rajendran4
                      wrote on last edited by Vinoth Rajendran4
                      #12

                      I got the point.

                      Virtual functions in inheritance hierarchy, stays virtual as long as the signature is same( return type not part of signature). But once signature varies, overloading comes into picture, and overloading rules apply for those functions.

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved