What is happening with this class

I don't understand this line of code:

Button::Button(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 150))

what is the wxFrame thing doing? is it inheriting? Constructing?

The code is from here (button.cpp):
http://zetcode.com/tutorials/wxwidgetstutorial/firstprograms/
It is constructing.

Specifically, the syntax that is confusing you is called an initializer list and is only usable in constructors.

To best illustrate initializer lists, the following two code segments do the exact same thing:

Given a class:

1
2
3
4
5
6
class Foo {
    int x;
    int y;
  public:
    Foo( int a, int b );
};


Code block 1:

1
2
3
4
Foo::Foo( int a, int b ) {
   x = a;
   y = b;
}


Code block 2:

1
2
3
4
Foo::Foo( int a, int b ) :
   x( a ), y ( b )
{
}



In the former case, x and y are first both default constructed (ie, their default constructors are run; since they are ints, the default constructor does nothing) and then x and y are assigned the values a and b respectively.

In the latter case, x and y are copy constructed (ie, the copy constructor for int is invoked) from a and b to assign values.

In your case, wxFrame is a data member of Button which is being "initialized" by calling one of its constructors.
Last edited on
Inherting and constructing.

Button::Button(const wxString& title)

is a constructor for the Button class, which is derived from the wxFrame base class. Basically, when a Button object is instantiated, it inherits the data members from the base class wxFrame. When the Button classes default or overloaded constructor is called, it must also call the base classes constructor be it default or overloaded, which is accomplished in this line:

wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 150))
Now would be a good time to recommend Effective C++ by Scott Meyers. It's a relatively short book that explains 55 (or 50 depending on edition) ways to use C++ more effectively. It is a great read for entry-level engineers that are familiar with C++ but want to learn more. As long as you are at the skill level that it targets, you will gain a lot in a very short time period.
Topic archived. No new replies allowed.