What is this? Really very easy to answer 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
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

What is this? Really very easy to answer

Post by parad0x13 » Fri Mar 06, 2009 10:05 pm

Code: Select all

MainFrm::MainFrm : wxFrame(NULL, wxID_ANY, title, pos, size)
I just want to know what the : is for, and why the wxFrame after it has no variable types inside, just referances

ninja9578
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Thu Jan 29, 2009 3:33 pm

Post by ninja9578 » Fri Mar 06, 2009 10:22 pm

C++ doesn't pass parameters up the inheritance structure because the child constructor may not have the same parameters as the parent. Using the : on the constructor lets you do that.

parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 » Fri Mar 06, 2009 10:32 pm

Oh... Thank you ninja9578

So if I understand correctly:

Code: Select all

MainFrm::MainFrm(const wxString &title, const wxPoint &pos, const wxSize &size)
	: wxFrame(NULL, wxID_ANY, title, pos, size)
wxFrame's title, position, and size will be inherited from MainFrm's title, position, and size

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Fri Mar 06, 2009 11:17 pm

No. You have it inverted. MainFrm will call the parent constructor and create a wxFrame. As a result the parameters that are passed to MainFrm are used to create the wxFrame.

For example:

Code: Select all

HelperFrame::HelperFrame(wxFrame *frame, const wxString& title)
    : wxFrame(frame, -1, title)
{
};
This will give the exact same outcome as using the create function after the function is called.

Code: Select all

HelperFrame::HelperFrame(wxFrame *frame, const wxString& title)
{
    wxFrame::Create(frame, -1, title);
}
These BOTH create a frame. Even the first one without a body statement creates a frame because it calls the parent constructor.

It does produce the same thing but they are not the same. One inherits the parents constructor and calls it. When you declare your function / constructor inside the header, you can place the inheritance there as well. The other calls the wxFrame creation function. Even if both of these functions are the exact same, it's also been proven through advanced benchmarking that inheritance like this is faster than calling outside functions inside the blocks.

If you want to know more or have a better explanation, please consult HERE

parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 » Fri Mar 06, 2009 11:23 pm

Thank you for your help, I will do the reasearch : ) !!!

Post Reply