Why virtual bool OnInit()? Why virtual? Topic is solved
Why virtual bool OnInit()? Why virtual?
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.
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.
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
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
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.
Thanks for reply.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
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.
-
- Ultimate wxWidgets Guru
- Posts: 536
- Joined: Fri Jan 07, 2005 3:44 pm
- Location: Finland
- Contact:
"MyApp" refers to the class that derives from wxApp:riruilo wrote:Why should i derieve from MyApp? Why not direcly from wxApp?
Code: Select all
class MyApp : public wxApp
{
...
};
HTH
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere
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().riruilo wrote:Why should i derieve from MyApp? Why not direcly from wxApp?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.
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.
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.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.
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.
Excuse me. I don't know how to accept all of them.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..
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.
When you view your topic, you have the option to choose Assist/Accept (the buttons are where your edit/delete/quote buttons are).riruilo wrote:Excuse me. I don't know how to accept all of them.
All your replies are very helpful for me.
Pick assisting answers first, then choose the best answer as the accepted answer.