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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on 11 Mar 2021, 03:40 last edited by
    #1

    Hi All,
    I know the fact that different parameter type in virtual function is not qualified for override, but how compiler was able to detect error for return type's type mismatch, whereas it's not able to find error with parameter type ?

    Thanks!

    J 1 Reply Last reply 11 Mar 2021, 04:01
    0
    • V Vinoth Rajendran4
      11 Mar 2021, 03:40

      Hi All,
      I know the fact that different parameter type in virtual function is not qualified for override, but how compiler was able to detect error for return type's type mismatch, whereas it's not able to find error with parameter type ?

      Thanks!

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 11 Mar 2021, 04:01 last edited by JKSH 3 Nov 2021, 04:12
      #2

      @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
      };
      

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

      V 1 Reply Last reply 11 Mar 2021, 04:11
      8
      • J JKSH
        11 Mar 2021, 04:01

        @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 11 Mar 2021, 04:11 last edited by Vinoth Rajendran4 3 Nov 2021, 04:14
        #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 ?

        J 1 Reply Last reply 11 Mar 2021, 04:19
        0
        • V Vinoth Rajendran4
          11 Mar 2021, 04:11

          @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 ?

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 11 Mar 2021, 04:19 last edited by JKSH 3 Nov 2021, 04:29
          #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 11 Mar 2021, 06:00
          7
          • J JKSH
            11 Mar 2021, 04:19

            @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 11 Mar 2021, 06:00 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 J 2 Replies Last reply 11 Mar 2021, 06:10
            1
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 11 Mar 2021, 06:06 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
                11 Mar 2021, 06:00

                @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 11 Mar 2021, 06:10 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 11 Mar 2021, 06:37
                7
                • V Vinoth Rajendran4
                  11 Mar 2021, 06:00

                  @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 ?

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 11 Mar 2021, 06:15 last edited by JKSH 3 Nov 2021, 06:25
                  #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 11 Mar 2021, 06:17
                  1
                  • J JKSH
                    11 Mar 2021, 06:15

                    @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 11 Mar 2021, 06:17 last edited by jsulm 3 Nov 2021, 06:18
                    #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

                    J 1 Reply Last reply 11 Mar 2021, 06:24
                    0
                    • jsulmJ jsulm
                      11 Mar 2021, 06:17

                      @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.

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 11 Mar 2021, 06:24 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
                        11 Mar 2021, 06:10

                        @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 11 Mar 2021, 06:37 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 11 Mar 2021, 06:43 last edited by Vinoth Rajendran4 3 Nov 2021, 07:02
                          #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

                          1/12

                          11 Mar 2021, 03:40

                          • Login

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