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
    23,787
    Yes (though you can change or omit their names if you want).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,787
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,787
    You need to specify return types too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,604
    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