Contents Up Previous Next

C++ issues

There are cases where a C++ program will compile and run fine under one environment, and then fail to compile using a different compiler. Some caveats are given below, from experience with the GNU C++ compiler (GCC) and MS C/C++ compiler version 7.

Templates
Definition of constructors
Pointers to functions
Precompiled headers


Templates

wxWindows does not use templates for two main reasons: one, it is a notoriously unportable feature, and two, the author is irrationally suspicious of them and prefers to use casts. More compilers are now implementing templates, and so it will probably be safe to use them soon without fear of portability problems.


Definition of constructors

Some compilers allows the user to omit constructor definitions where a parent class provides a constructor with parameters. In Microsoft C++, all constructors with parameters must be defined in the derived class, or the compiler cannot find the required constructor. This may mean defining dummy constructors which call parent constructors, for example:

MyClass::MyClass(int x, int y):ParentClass(x, y)
{
}
This is not a problem where the constructor has no parameters.


Pointers to functions

Some compilers are clever in their matching of function pointer arguments to the declaration of the function, and will not complain in the following case:

typedef void (*wxFunction) (wxObject&, wxCommandEvent&);
...
void mycallback(wxButton& button, wxCommandEvent& event);
...
wxButton button(parent, &mycallback, label, 100, 200);
Since wxButton is derived from wxObject, the function mycallback is a subtype of wxFunction. In Microsoft C++, and most compilers with a high warning level set, the function is not an exact match, and the compiler complains. The solution is to place a cast in front of the function address, thus:

wxButton button(parent, (wxFunction)&mycallback, label, 100, 200);

Precompiled headers

Some compilers, such as Borland C++ and Microsoft C++, support precompiled headers. This can save a great deal of compiling time. The recommended approach is to precompile "wx.h'', using this precompiled header for compiling both wxWindows itself and any wxWindows applications. For Windows compilers, two dummy source files are provided (one for normal applications and one for creating DLLs) to allow initial creation of the precompiled header.

However, there are several downsides to using precompiled headers. One is that to take advantage of the facility, you often need to include more header files than would normally be the case. This means that changing a header file will cause more recompilations (in the case of wxWindows, everything needs to be recompiled since everything includes "wx.h''!)

A related problem is that for compilers that don't have precompiled headers, including a lot of header files slows down compilation considerably. For this reason, you will find (in the common X and Windows parts of the library) conditional compilation that under UNIX, includes a minimal set of headers; and when using Visual C++, includes wx.h. This should help provide the optimal compilation for each compiler, although it is biassed towards the precompiled headers facility available in Microsoft C++.