Why virtual bool OnInit()? Why 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
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Why virtual bool OnInit()? Why virtual?

Post by riruilo »

Hi friends!

Just a easy question (for you). Why do you declare OnInit as virtual?

I started with wxWidgets reading this
http://www.wxwidgets.org/docs/tutorials/hello.htm
and I don't understand why oninit is virtual.

Thanks a lot.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway »

Hi,

It's virtual because if you derive your own class from wxApp, you can override the default OnInit method with one of your own if you choose.


See http://www.cplusplus.com/doc/tutorial/polymorphism.html

Jim
OS: Vista SP1, wxWidgets 2.8.7.
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post by riruilo »

JimFairway wrote:Hi,

It's virtual because if you derive your own class from wxApp, you can override the default OnInit method with one of your own if you choose.


See http://www.cplusplus.com/doc/tutorial/polymorphism.html

Jim
Thanks for reply.

Why should i derieve from MyApp? Why not direcly from wxApp?

Thanks.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
jacmoe
Experienced Solver
Experienced Solver
Posts: 64
Joined: Sun Feb 22, 2009 3:34 pm
Location: Denmark
Contact:

Post by jacmoe »

riruilo wrote:Why should i derieve from MyApp? Why not direcly from wxApp?
What he said. :wink:
vdell
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell »

riruilo wrote:Why should i derieve from MyApp? Why not direcly from wxApp?
"MyApp" refers to the class that derives from wxApp:

Code: Select all

class MyApp : public wxApp
{
...
};
Naturally the class can be called whatever you want.

HTH
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

riruilo wrote:
JimFairway wrote:It's virtual because if you derive your own class from wxApp, you can override the default OnInit method with one of your own if you choose.
Why should i derieve from MyApp? Why not direcly from wxApp?
JimFairway is right, but riruilo asks a good question : OnInit() is declared virtual in class wxApp to ensure that MyApp use the "last" form of OnInit().
But there's no reel need to declare OnInit() virtual into MyApp. It's only usefull if MyApp is also derived.

Answer is: as nobody knows what can be future uses of this class, declaring OnInit() allows it to be overriden. But if you're 100% sure you'll never derive from MyApp, you can declare OnInit() as non-virtual.
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank »

A function that is declared as virtual is always virtual. In every derived class. whether you explicitly write "virtual" in your derived class or not.

So if you need a "virtual" to remind you, that this function is virtual use it. If not, don't. Makes no difference.
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

Frank wrote:A function that is declared as virtual is always virtual. In every derived class. whether you explicitly write "virtual" in your derived class or not.

So if you need a "virtual" to remind you, that this function is virtual use it. If not, don't. Makes no difference.
I made the test. It's right. I didn't know that. So, virtual or no virtual, that's same thing for users of wxApp class.
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post by riruilo »

Frank wrote:A function that is declared as virtual is always virtual. In every derived class. whether you explicitly write "virtual" in your derived class or not.

So if you need a "virtual" to remind you, that this function is virtual use it. If not, don't. Makes no difference.

So, do you say OnInit in wxApp is virtual, but people usually write virtual on their derived class to remember it is a virtual func? Am I right?
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

Exactly
Jérémie
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post by riruilo »

Thanks a lot. Now it is clear.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
jacmoe
Experienced Solver
Experienced Solver
Posts: 64
Joined: Sun Feb 22, 2009 3:34 pm
Location: Denmark
Contact:

Post by jacmoe »

jfouche's answer is correct, but I find it weird that you accept his answer, and not the others who helped you out.. :roll:
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post by riruilo »

jacmoe wrote:jfouche's answer is correct, but I find it weird that you accept his answer, and not the others who helped you out.. :roll:
Excuse me. I don't know how to accept all of them.
All your replies are very helpful for me.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
jacmoe
Experienced Solver
Experienced Solver
Posts: 64
Joined: Sun Feb 22, 2009 3:34 pm
Location: Denmark
Contact:

Post by jacmoe »

riruilo wrote:Excuse me. I don't know how to accept all of them.
All your replies are very helpful for me.
When you view your topic, you have the option to choose Assist/Accept (the buttons are where your edit/delete/quote buttons are).
Pick assisting answers first, then choose the best answer as the accepted answer. :)
Post Reply