Thread: Why must virtual function be implemented?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Why must virtual function be implemented?

    When I try to compile this:

    Code:
    class Base {
    public:
        virtual void functionA() {cout<<"Base::functionA"<<endl;}
        virtual void functionB(){cout<<"Base::functionB"<<endl;}
        virtual ~Base();
    };
    
    class Derived1: public Base {
    public:
        virtual void functionA() {cout<<"Derived1::functionA"<<endl;}
    };
    It does not compile, saying:
    Code:
    undefined reference to `vtable for Base'
    undefined reference to `Base::~Base()'
    However, when I change the above to this:

    Code:
    class Base {
    public:
        virtual void functionA() {cout<<"Base::functionA"<<endl;}
        virtual void functionB(){cout<<"Base::functionB"<<endl;}
        virtual ~Base() {} // notice empty implementation added
    };
    
    class Derived1: public Base {
    public:
        virtual void functionA() {cout<<"Derived1::functionA"<<endl;}
    };
    It compiles just fine.
    Why must a virtual function be implemented, but non virtual function can stay unimplemented as long as you don't call them, of course...

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Why must virtual function be implemented?
    O_o

    The fact is that virtual functions in general don't have to be implemented.

    You can search for "pure virtual" to find the information.

    The destructor is kind of unique. Simply put, a destructor for a class is always called at the end of an object's lifetime.

    A `Derived1` object is also object `Base` object so the lifetime of a `Derived1` is also the lifetime of a `Base` object.

    The `Derived1` class destructor is calling the `Base` class destructor even if you can't see the code.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Absurd
    Why must a virtual function be implemented, but non virtual function can stay unimplemented as long as you don't call them, of course...
    The exact requirement of the standard is actually:
    Quote Originally Posted by C++11 Clause 10.3 Paragraph 11
    A virtual function declared in a class shall be defined, or declared pure in that class, or both; but no diagnostic is required.
    I believe the "no diagnostic is required" part means that you're pretty much in the realm of undefined behaviour when you do declare a non-pure virtual function and yet fail to define it, and in this case your compiler/linker has chosen to issue a diagnostic.

    As for why this rule exists: it is probably a consideration for expected implementations, but you'll need someone more familiar with these details to explain exactly why.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with virtual function and virtual inheritance
    By shaxquan in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2010, 10:39 AM
  2. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  3. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  4. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  5. Confusing between Virtual and Non-virtual function
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2006, 06:30 PM