Using CodeBlocks 10.05 – Part 5

This post begins with the code found in Book IV Chapter 3 (found in \Author\BookIV\Chapter03) of my book,  C++ All-In-One Desk Reference For Dummies. I’ve been testing my code for use with CodeBlocks 10.05 continues. I introduced this new version in Using CodeBlocks 10.05 – Part 1, where I discussed the general look of the new version.

The example on page 456 (example Constructor04) is supposed to fail. It’s an example of what happens when you add a specific constructor to a class without also defining a default constructor. CodeBlocks 10.05 fails in the same way that its predecessor does. However, the order of the candidates may differthe Simple::Simple(int) version may appear first. I’ve pointed out a few other error message changes in other posts (you can see the entire list of posts for this book in the Category Archive for C++ All-in-One for Dummies).

Otherwise, all of the other examples in Book IV Chapter 3 should work as described in the book. Since there was little to talk about in Book IV Chapter 3, let’s also discuss Book IV Chapter 4.

The example that begins on page 482 (example OverridingDerived, found in \Author\BookIV\Chapter04) of the book compiles with three warning messages in CodeBlocks 10.05 as shown here:

 

C:\Author\BookIV\Chapter04\OverridingDerived\main.cpp: In constructor ‘Printer::Printer(std::string, Printer::PrinterType, int, int)’:

C:\Author\BookIV\Chapter04\OverridingDerived\main.cpp:19: warning: ‘Printer::Type’ will be initialized after

C:\Author\BookIV\Chapter04\OverridingDerived\main.cpp:22: warning:   base ‘Peripheral’

C:\Author\BookIV\Chapter04\OverridingDerived\main.cpp:20: warning:   when initialized here

Linking console executable: bin\Debug\OverridingDerived.exe
Output size is 1000.61 KB
Process terminated with status 0 (0 minutes, 2 seconds)
0 errors, 3 warnings

CodeBlocks 10.05 performs some additional checks that CodeBlocks 8.02 didn’t, so it will catch problems like the one shown in this listing. The example still works as described in the book. To get rid of the warning messages, you need to change the order of inheritance for the Printer class as shown here:

class Printer : public Peripheral {
public:
    enum PrinterType {laser, inkjet};
    PrinterType Type;
    Printer(string aname, PrinterType atype, int aprice,
        int aserial) :
        Peripheral(aname, aprice, aserial), Type(atype) {}
};

The idea is to ensure initialization takes place in the correct order. Even though the initialization order doesn’t make a difference in this example, it could in code you write. Notice that Type(atype) now appears after Peripheral(aname, aprice, aserial), which provides the correct initialization order.

In the next post I’ll start with Book IV Chapter 5. In the meantime, let me know if you have any questions about the CodeBlocks 10.05 updates for this book at John@JohnMuellerBooks.com.