Limitation of virtual functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nagesh0280
    New Member
    • Mar 2008
    • 4

    Limitation of virtual functions?

    Hello Experts,

    I was wondering (I'm absolutely new to OOP) if virtual functions are limited in their capability in a sense? Consider the following scenario.

    #include <iostream>

    using namespace std;

    class base
    {
    public:
    virtual int add (int a, int b) {
    cout << "Result = " << a + b << endl;
    return a + b;
    }
    };

    class derived_float : public base
    {
    public:
    float add (float a, float b) {
    cout << "Result = " << a + b << endl;
    return a + b;
    }
    };

    int main() {

    derived_float dp;
    base * bp = &dp;
    bp->add (2.5, 4.6);
    return(0);
    }

    I basically can't do this. What I see is,

    [nageshg@lx-nageshg1 nageshg]$ g++ test.cpp
    test.cpp: In function 'int main()':
    test.cpp:27: warning: passing 'double' for argument 1 to 'virtual int base::add(int, int)'
    test.cpp:27: warning: passing 'double' for argument 2 to 'virtual int base::add(int, int)'

    [nageshg@lx-nageshg1 nageshg]$ l
    a.out test.cpp test.sv

    [nageshg@lx-nageshg1 nageshg]$ ./a.out
    Result = 6

    It would have been more powerful if virtual functions which are overridden in derived classes, could behave like overloaded functions. From the current scenario it seems to me that virtual functions are useful for scenarios with,

    Same return type
    Same argument data types
    Same number of arguments

    Looks like limited capability. Am I missing anything? The code above is contrived and may not be the best way to express my thoughts. Please excuse...

    Thanks,
    Nagesh
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Because overloaded functions are resolved at compile time, and compiler can select matching function and matching convertors for all arguments if types don't match exactly, but can be converted. Virtual functions are resolved at runtime, ( using pointers to functions ) and caller expect similar arguments list and expect all called functions recognize them correctly.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by nagesh0280
      Am I missing anything?
      Yes you are.

      When you declare a function in a derived class that has the same name as the base class function but has different arguments, the derived function dominates.

      This has nothing to do with virtual. It has to do with the rule of dominance. Consider:

      Code:
      int i = 5;
      int main()
      {
          int i = 10;
          cout << i;        //you see 10
      }

      Here the local i dominates the global i. It's the same with functions. The local name dominates. What this tells you is that you cannot overload functions between classes. You have to do your overloading in the same class, that is, in the same scope.

      The rule of domination is based solely on the name of the function. Arguments are ignored and so is the public/privated/protected specifiers.

      All the virtual does is tell the compiler that if you use a base pointer with a derived object and the derived function has the same function name and arguments as the base class function, then call the derived class function.

      Use a derived pointer with a derived object and you call the derived method regardless of whether it is in the base class or not or virtual or not. That's because the deirved methods dominate.

      Comment

      Working...