virtual bool OnInit()? 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
satchmo
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jan 22, 2006 5:37 pm
Location: Dublin, Ireland

virtual bool OnInit()?

Post by satchmo »

Hi there,
I know this is a pretty basic C++ question, but I've just started learning to use wxWidgets and am a little confused about the inheritance aspect of the OnInit function in most examples.

I understand that OnInit is a wxApp function and a virtual one, which needs to be overridden in order to start an application. It's my understanding that when a function is to be overridden it is declared in the base class as virtual, but when actually being overridden is declared as normal (ie without the virtual). However in all the wxWidgets examples I see, when the class that inherits from wxApp is being declared, they still declare OnInit as virtual, although then the implementation of that class is as normal. Eg:

Code: Select all

class MyApp : public wxApp{
    public:
        virtual bool OnInit();  //why not just "bool OnInit();"?
};

bool MyApp::OnInit(){
    //stuff
}
It compiles and runs fine with or without the virtual, so I'm a little confused as to what the difference is?

Thanks,
satch
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

This is the normal C++ definition.
When a base-class function is declared as virtual, all overriden implementation are virtual. With or without the virtual keyword.

If you want a function with the same prototype but not virtual, you must change its name.
What is little and green, witch go up and down ??
Yoda playing with the force.
chris
I live to help wx-kind
I live to help wx-kind
Posts: 150
Joined: Fri Oct 08, 2004 2:05 pm
Location: Europe

Post by chris »

Hi satchmo,

on a further note: Virtual methods that need to be overwritten are called "pure" virtual methods in C++, and are declared by

Code: Select all

virtual void foo()=0;
(note the =0).
Classes with pure virtual methods are abstract and can not be instantiated. Any class that derives from a class with pure virtuals must implement these method to be instantiable.

HTH, Chris

edit: The reason why some people add the virtual keyword in derived classes even if it's redundant is readability. If you as the programmer read the declaration you know you are dealing with a potentially overwritten/overwritable method.
this->signature=NULL;
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

Cursor wrote: If you want a function with the same prototype but not virtual, you must change its name.
You can also only change the parameters, but then that results in the phenomena known as function hiding, which is usually not a good thing...
[Mostly retired moderator, still check in to clean up some stuff]
satchmo
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jan 22, 2006 5:37 pm
Location: Dublin, Ireland

Post by satchmo »

Cursor wrote:This is the normal C++ definition.
When a base-class function is declared as virtual, all overriden implementation are virtual. With or without the virtual keyword.
So you mean that even if the virtual keyword is left out, it's still virtual? That even without the keyword, any class that inherits from MyApp will still have a virtual function bool OnInit() too?
chris
I live to help wx-kind
I live to help wx-kind
Posts: 150
Joined: Fri Oct 08, 2004 2:05 pm
Location: Europe

Post by chris »

Yes, that's correct.
this->signature=NULL;
satchmo
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jan 22, 2006 5:37 pm
Location: Dublin, Ireland

Post by satchmo »

Okay, thanks guys.
Post Reply