Thread: Extending Classes without Virtual Methods

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Question Extending Classes without Virtual Methods

    A quick clarification on virtual methods after reading Jumping int C++ by Alex Allain. If a user wanted to extend a class from someone elses library and override its methods that do not contain virtual methods; how would one call the overridden class if it is referred to by its super type

    in other words how would someone override a method from someone elses library that does not have virtual keywords.

    ie
    something.h
    Code:
    #include <iostream>
    
    namespace game{
                      class character{
                                public:
                                          std::string getName(){return "character";}
                       };
    }
    main.cpp
    Code:
    #include <iostream>
    #include <vector>
    #include "something.h"
    
    using std::vector
    
    class protagonist : game::character{
             public:
                       virtual std::string getName(){return "protagonist";}
    };
    
    class protagonist : game::protagonist{
             public:
                     std::string getName(){return "antagonist";}
    };
    
    
    int main(){
             
         vector<character*> vec;
         vec.push_back(new protagonist);
         vec.push_back(new antagonist);
             
          for(vector<Character*>::iterator itr = vec.begin(),
                          end = vec.end(); 
                          itr!=end; 
                          ++itr){
            std::printf("%s", (*itr)->getName()); 
            delete (*itr);
            cout << endl << endl;
        }
        vec.clear();
    
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Overriding a method does not require that the base class use the virtual keyword. If you override the base class method, the base version is hidden and won't be called unless you explicitly invoke 'Base::foo()'. The virtual keyword makes it so that you can use polymorphism, but you can always use a derived object in a normal way and not a polymorphic way.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bonechilla
    If a user wanted to extend a class from someone elses library and override its methods that do not contain virtual methods; how would one call the overridden class if it is referred to by its super type

    in other words how would someone override a method from someone elses library that does not have virtual keywords.
    If the class has no virtual member functions (i.e., methods), then it is not intended to be a polymorphic base class, at least not via the runtime polymorphism enabled by the use of inheritance with virtual functions. Thus, by design, you would not derive from it in order to override any methods since there are no methods to override. Any attempt to override would actually result in overloading instead.

    EDIT:
    Quote Originally Posted by whiteflags
    Overriding a method does not require that the base class use the virtual keyword.
    This is false by definition:
    overriding - declaring a function in a derived class with the same name and a matching type as a virtual function in a base class. The argument types must match exactly. The return types must match exactly or be co-variant. The overriding function will be invoked when the virtual function is called.
    Last edited by laserlight; 03-11-2014 at 12:40 PM.
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the base class's function isn't virtual, then it's just not possible. You would have to make your own "wrapper" which contains the necessary virtual functions which you could inherit from.

    Also, do not use printf in C++. There is a I/O out object. It's called std::cout.

    >>vector<character*> vec;
    >> vec.push_back(new protagonist);
    >> vec.push_back(new antagonist);
    I suggest you change that to
    vector<std::unique_ptr<character>> vec;
    vec.emplace_back(new protagonist);
    vec.emplace_back(new antagonist);

    Code:
          for(vector<Character*>::iterator itr = vec.begin(),
                          end = vec.end();
                          itr!=end;
                          ++itr){
            std::printf("%s", (*itr)->getName());
            delete (*itr);
            cout << endl << endl;
        }
    Code:
          for(auto Character : vec)
            std::cout << Character->getName()) << "\n";
    >>vec.clear();
    Not necessary. Redundant.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Thumbs up

    Quote Originally Posted by laserlight View Post
    If the class has no virtual member functions (i.e., methods), then it is not intended to be a polymorphic base class, at least not via the runtime polymorphism enabled by the use of inheritance with virtual functions. Thus, by design, you would not derive from it in order to override any methods since there are no methods to override. Any attempt to override would actually result in overloading instead.
    Thank you for the quick reply i figured but was not sure exactly what was happening.

    Quote Originally Posted by Elysia View Post
    If the base class's function isn't virtual, then it's just not possible. You would have to make your own "wrapper" which contains the necessary virtual functions which you could inherit from.
    Code:
          for(vector<Character*>::iterator itr = vec.begin(),
                          end = vec.end();
                          itr!=end;
                          ++itr){
            std::printf("%s", (*itr)->getName());
            delete (*itr);
            cout << endl << endl;
        }
    Code:
          for(auto Character : vec)
            std::cout << Character->getName()) << "\n";
    Thank you Elysia i was unaware of the unique_ptr and was under the assumption that C++ did not have a for each loop.
    Last edited by bonechilla; 03-11-2014 at 11:37 PM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thank you Elysia i was unaware of the unique_ptr and was under the assumption that C++ did not have a for each loop.
    It didn't until the latest revision of the standard published in 2011, so you won't find that in the wild too much. Most companies/projects haven't made the switch, yet.

    unique_ptr (and other smart pointers) are also C++11 additions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pure virtual methods help
    By makr28 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2011, 03:39 PM
  2. inheritance and virtual methods
    By zxcv in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2009, 09:03 AM
  3. Problem With Virtual Methods
    By frassunit in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2009, 07:55 PM
  4. Not 100% sure when to use virtual methods in base class
    By Silvercord in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 03:19 PM
  5. virtual methods
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 02-16-2002, 07:44 PM

Tags for this Thread