Virtual Function and Its formal parameters

This is a discussion on Virtual Function and Its formal parameters within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if the formal parameters must remain the same in a class' virtual member function and that ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    Virtual Function and Its formal parameters

    Hi,
    I was wondering if the formal parameters must remain the same in a class' virtual member function and that of its derived class? Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    Yes (though you can change or omit their names if you want).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    In this example:
    Code:
    class A{
    virtual f(int a, float b, double c)
    }
    
    class B: public A{
     ...
    }
    So B: f(int a, float b, double c, char* d) is not correct, but B:f(int b, float , double c) is?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    Yes, at least if your intention is to override the virtual function instead of overload with another member function named f (and this overload would cause the inherited f to be hidden).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks!

    So this is overload
    Code:
    class A{
    virtual f(int a, float b, double c)
    }
    
    class B: public A{
     f(int a, float b, double c, char* d) // overload A::f
    }
    and this is override
    Code:
    class A{
    virtual f(int a, float b, double c)
    }
    
    class B: public A{
    f(int b, float , double c) // override A::f
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    You need to specify return types too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Oops.
    Thank you!

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    And now for the real question. Why do you think this is the case? The answer will show whether or not you understand what is going on. Just knowing the answer is not good enough. You must understand why.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21