Trying as little code as possible, doubt with virtual Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Nick
Knows some wx things
Knows some wx things
Posts: 37
Joined: Thu Apr 11, 2019 12:50 am

Trying as little code as possible, doubt with virtual

Post by Nick » Fri Aug 16, 2019 10:12 pm

Why put the word "Virtual" If it works without it?
What does it mean "Virtual" ?

Code: Select all

#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);

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Trying as little code as possible, doubt with virtual

Post by doublemax » Fri Aug 16, 2019 11:02 pm

Is this context it's only a hint to the reader of this code that a method of a base class get overridden. It has no syntactical meaning.
Use the source, Luke!

alys666
I live to help wx-kind
I live to help wx-kind
Posts: 191
Joined: Tue Oct 18, 2016 2:31 pm

Re: Trying as little code as possible, doubt with virtual

Post by alys666 » Fri Aug 16, 2019 11:06 pm

it just means that your c++ compiler treats a method, not explicitly declared virtual, as virtual, because base class has virtual with the same signature.
do not experiment with c++.
if base class gives you abstract or virtual method, and you must override it, you MUST EXPLICITLY override it.
here you must write your specific method as -

Code: Select all

bool OnInit() override
{
...
}
specifier "override" says to reader and compiler that you are explicitly overriding method of base class.
write "override" in every function, where you are overriding. in this case excessive "virtual" can be omitted.
ubuntu 16.04, wxWidgets 3.0.4

Nick
Knows some wx things
Knows some wx things
Posts: 37
Joined: Thu Apr 11, 2019 12:50 am

Re: Trying as little code as possible, doubt with virtual

Post by Nick » Sat Aug 17, 2019 1:38 am

alys666 wrote:
Fri Aug 16, 2019 11:06 pm
it just means that your c++ compiler treats a method, not explicitly declared virtual, as virtual, because base class has virtual with the same signature.
do not experiment with c++.
if base class gives you abstract or virtual method, and you must override it, you MUST EXPLICITLY override it.
here you must write your specific method as -

Code: Select all

bool OnInit() override
{
...
}
specifier "override" says to reader and compiler that you are explicitly overriding method of base class.
write "override" in every function, where you are overriding. in this case excessive "virtual" can be omitted.
Thank you for the explanation!

Post Reply