Overriding

This is a discussion on Overriding within the C++ Programming forums, part of the General Programming Boards category; Suppose I have class in a library whose method i wish to override, but it is not declared virtual How ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Overriding

    Suppose I have class in a library whose method i wish to override, but it is not declared virtual

    How can I go about overriding the method?

    all methods in Java are virtual so, no such problem occurs there

    C# provides an explicit new keyword for the same problem

    I expect C++ didnt had that feature... but still ready to for a strange answer

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    I don't believe that you can do this.

    Though if you have a child object and call foo(), it will call the child object's foo function even if the parent class has an equivalent foo function defined.

    The problem you'll run in to is that if you have a pointer to a base class that points to a child class, calling foo will always call the base class's implementation.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by jverkoey View Post
    The problem you'll run in to is that if you have a pointer to a base class that points to a child class, calling foo will always call the base class's implementation.
    yeah, bcoz it was never overriden!

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    So there you go.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You can override any base function, but if it's not virtual, then you won't have polymorphism, however you can make a buffer class, for instance:

    Code:
    #include <iostream>
    #include <string>
    
    
    class base
    {
    public:
         std::string somenonvirtualfunction()
         {
              return "Base";
         }
    };
    
    class newbase : public base
    {
    public:
         virtual std::string somenonvirtualfunction()
         {
              return base::somenonvirtualfunction();
         }
    };
    
    class derived : public newbase
    {
    public:
         virtual std::string somenonvirtualfunction()
         {
              return "Derived";
         }
    };
    int main()
    {
    newbase* a = new newbase;
    newbase* b = new derived;
    
    std::cout << a->somenonvirtualfunction() << "\n";
    std::cout << b->somenonvirtualfunction() << "\n";
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function overriding
    By csvraju in forum C++ Programming
    Replies: 1
    Last Post: 06-11-2009, 07:01 AM
  2. Overriding operator new/delete?
    By Elysia in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2008, 12:10 PM
  3. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 01:26 AM
  4. overloading and overriding
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 03:29 PM
  5. redefining and overriding members
    By strobe9 in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2002, 03:43 PM

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