How to override Show() method? 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
djmig
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 10, 2016 11:18 pm

How to override Show() method?

Post by djmig »

Hi,
I've been trying to override Show() method from a class derived from wxFrame, but could not get it to work, possibly due to my incompetence in both C++ and understanding of wxWidgets in general :(

Suppose I have the following class header:

Code: Select all

#include <wx/wx.h>

class SuperDumbFrame : public wxFrame
{
public:
    SuperDumbFrame();
    bool Show(bool) wxOVERRIDE;
    wxFrame *dumbframe;
};
And the following cpp:

Code: Select all

#include "dumbframe.h"

SuperDumbFrame:: SuperDumbFrame()
        : wxFrame(NULL, wxID_ANY, wxEmptyString, wxDefaultPosition)
{
    dumbframe = this;
}

bool SuperDumbFrame::Show(bool show=true)
{
    std::cout << "in show ..." << std::endl;
    return dumbframe->Show(show);
}
And the following app:

Code: Select all

#include <wx/wx.h>

#include "dumbframe.h"

class MyApp : public wxApp
{
public:
	bool OnInit();
};

class MyFrame : public SuperDumbFrame
{
public:
    MyFrame();
    ~MyFrame();
};

bool MyApp::OnInit()
{
    MyFrame *myframe = new MyFrame();
	myframe->Show(true);
	return true;
}

MyFrame::MyFrame()
			: SuperDumbFrame()
{
	
}

MyFrame::~MyFrame()
{
	
}

IMPLEMENT_APP( MyApp )
What is the proper way to override Show()?
Your help is very much appreciated.
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to override Show() method?

Post by doublemax »

It should work how you did it.

I tested with this and it worked:

Code: Select all

class SuperDumbFrame : public wxFrame
{
public:
    SuperDumbFrame() : wxFrame(NULL, wxID_ANY, "SuperDumbFrame")
    {
    }

    virtual bool Show(bool show = true)
    {
      wxLogDebug("SuperDumbFrame::Show");
      return wxFrame::Show(show);
    }
};

class MyFrame : public SuperDumbFrame
{
public:
  MyFrame() : SuperDumbFrame()
  {
  }
};

bool MyApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    SuperDumbFrame *frame = new SuperDumbFrame();
    frame->Show(true);

    return true;
}
However, overriding Show() is very unusual, whatfor do you need this?
Use the source, Luke!
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: How to override Show() method?

Post by coderrc »

djmig wrote:Hi,
I've been trying to override Show() method from a class derived from wxFrame, but could not get it to work, possibly due to my incompetence in both C++ and understanding of wxWidgets in general :(

Code: Select all

bool SuperDumbFrame::Show(bool show=true)
{
    std::cout << "in show ..." << std::endl;
    return dumbframe->Show(show);
}
its not a wonder that this doesnt work. It's infunitely recursive.

You probably to call the the PARENT class's show, rather than having the show function return a call to itself. see doublemax's reply.
djmig
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 10, 2016 11:18 pm

Re: How to override Show() method?

Post by djmig »

@doublemax: thank you, that worked and made me rethink the program flow. My program was actually calling Objective-C method(s) from C++, and I was attempting to call Show() from within the .mm file. Evoking wxFrame::Show() there causes "error: call to non-static member function without an object argument" message to appear at compile time. Moving that wxFrame::Show() to within the C++ part solved the issue.

@coderc: quite true, it was indeed infinitely recursive. :(

Thank you both for the help.
Cheers
Post Reply