Thread: Trying as little code as possible, doubt!

  1. #1
    Registered User marcelo.br's Avatar
    Join Date
    Nov 2018
    Posts
    45

    Question Trying as little code as possible, doubt!

    Why put the word "Virtual" If it works without it?
    What does it mean "Virtual" ?
    Code:
    #include <wx/app.h>
    #include <wx/frame.h>
    
    class FrmMain: public wxFrame {
    public:
       FrmMain(): wxFrame(NULL, wxID_ANY, "2019") {
       }
    };
    
    class MyProgram: public wxApp {
       // virtual bool OnInit() { // Why put the word "Virtual" If it works without it?
       bool OnInit() {
          (new FrmMain)->Show();
          return true;
       }
    };
    
    wxIMPLEMENT_APP(MyProgram);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    wxWidgets is C++, so this thread shall be moved to the C++ programming forum.

    As for your question: you need to learn about inheritance and polymorphism in C++. This is typically covered by introductory material on C++ classes.
    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

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    214
    With full acknowledgement of @laserlight's point, the declaration of wxApp:OnInit is declared as virtual, so the function in the derived class will be virtual. While it is optional, there's more to the story.

    In C++11 the keyword override is used to declare the derived OnInit to indicate the author (that's you in this case) assumes the base declared that function is virtual (and thus issue an compile time error if that is not true).

    Using override is a good idea to ensure that you are getting what you expect, that in this case a call to the base OnInit will call your derived version through the virtual function mechanism. Without the specification of override, you may not recognize minor differences in the declaration which effectively disconnect the virtual mechanism.

    The older style, which wxWidgets is written to support (for portability among a great many compilers, even much older ones), left some ambiguity which brought up subtle bugs.

    So, it isn't so much whether you declare this function as virtual or not - if the signature is the exact match, the base makes it virtual (your use of it is merely an agreement of this fact in your code), it is whether or not you recognize that you are expecting and dependent upon the virtual call mechanism to work in order to call your OnInit, as opposed to some default in the base.

    The more modern use of override is, therefore, capable of helping you ensure that the override is correctly matching the base virtual function as you should expect.

    This is where @laserlight's point is key. It is something you must learn to recognize as you consume libraries which depend upon the virtual function mechanism for extension, as wxWidgets (and many others) does.

  4. #4
    Registered User marcelo.br's Avatar
    Join Date
    Nov 2018
    Posts
    45

    Cool

    Quote Originally Posted by laserlight View Post
    wxWidgets is C++, so this thread shall be moved to the C++ programming forum.
    I'm sorry, that was a lack of attention! I know the difference between C and C ++ and I believed I was even posting to the wxWidgets forum itself. Thanks for correcting by moving the topic!

  5. #5
    Registered User marcelo.br's Avatar
    Join Date
    Nov 2018
    Posts
    45
    Quote Originally Posted by Niccolo View Post
    With full acknowledgement of @laserlight's point, the declaration of wxApp:OnInit is declared as virtual, so the function in the derived class will be virtual. While it is optional, there's more to the story........
    Thanks for the info, they are very useful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program doubt on the below code.
    By ramkumar15 in forum C Programming
    Replies: 1
    Last Post: 03-30-2015, 12:42 AM
  2. doubt in c++ code, urgent please
    By leo2008 in forum C++ Programming
    Replies: 24
    Last Post: 03-13-2013, 02:43 AM
  3. Doubt regarding C++ code in old textbooks
    By thefeedinghand in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2010, 03:43 PM
  4. Code Doubt?
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 07-21-2008, 01:02 AM
  5. doubt in C--(reducing the length of code)
    By nardneran in forum C Programming
    Replies: 44
    Last Post: 10-15-2006, 02:38 PM

Tags for this Thread