Is it required to provide default definitions for virtual functions in a base class?

This is a discussion on Is it required to provide default definitions for virtual functions in a base class? within the C++ Programming forums, part of the General Programming Boards category; 1. Are we required to provide default definitions for (non-pure) virtual functions in a base class? My intuition tells me ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Is it required to provide default definitions for virtual functions in a base class?

    1. Are we required to provide default definitions for (non-pure) virtual functions in a base class? My intuition tells me yes because from what I understand a base class with virtual functions (and no pure virtual functions) is just like any other class save for the fact that its virtual functions may be re-defined by its child classes... But I want to double check this with you guys.

    2. If a class contains X functions and one of the X functions is pure virtual, then the class automatically becomes an abstract base class and objects of the class cannot be instantiated. If this is the case, then is it necessary to provide definitions for the other virtual functions that are not pure virtual?

    I know these questions seem useless since I can't think of a reason to not want to provide default definitions for virtual functions either way, but I'm preparing for a theory mid-term and I think it would be good to know these kinds of things for the test...

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Both of your questions are easily tested. What results do you get when you write a quick test program, and how are those results not what you expected?
    My best code is written with the delete key.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Isn't that the difference between pure virtual and non-pure virtual functions? Pure virtual function is a virtual function with no body. So if a virtual function has no body then it would be pure.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    No, a pure virtual function is a virtual function that must be overriden in derived classes. A pure virtual function will make the class abstract (thus it cannot be instantiated).
    However, most pure virtual functions still require a body (even if nonsensical), or you will get a linking error.
    And a pure virtual function is made by appending = 0 after the parameter list; it is not depending on whether the virtual function has a body or not.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote Originally Posted by Elysia
    However, most pure virtual functions still require a body (even if nonsensical), or you will get a linking error.
    A pure virtual function will only require a definition to avoid a linker error if it is called, e.g., a pure virtual destructor would be called by the destructors of derived classes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    I am uncertain of that, which is why I labeled it "most."
    I have certainly gotten linker errors from pure virtual functions not being defined, and yet, they are all overriden in the derived classes and as the base class itself cannot be instantiated, I fail to see how those (pure) virtual functions are being called at all.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Then you apparently have some other problem in your code. Could you post an example of when you think you are getting this error?

    In the general case a pure virtual function does not need to be defined. A pure virtual destructor must be defined because it is always called.

    Most pure virtual functions are not defined (except destructors). Even advanced programmers are surprised when they find out you can define a pure virtual function because it's just not done very often.


    Canadian0469, the answer to your first question is yes. It's hard to test to find out if it is required in all cases (you can't try all cases), so I'll just give you that answer. Have you tried the second problem?
    Last edited by Daved; 11-17-2008 at 10:59 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Quote Originally Posted by Daved View Post
    Then you apparently have some other problem in your code. Could you post an example of when you think you are getting this error?
    No, sorry. Too long ago.
    If I do reproduce it, I'll try to remember to post it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM
  3. base and derived class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 03:11 PM
  4. Replies: 1
    Last Post: 11-27-2001, 01:07 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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