Function Specifiers

C++ defines three function specifiers: inline, virtual, and explicit. The inline specifier is also supported by C99. inline is a request to the compiler to expand a function’s code inline rather than to call it. If the compiler cannot inline the function, it is free to ignore the request. Both member and nonmember functions may be specified as inline.

Programming Tip 

There are two ways a constructor can initialize member variables. First, it can explicitly assign them values inside the body of the constructor. For example, consider MyClass, shown here, which has two integer data members called numA and numB. These member variables are initialized inside the MyClass constructor.

class MyClass {    int numA;    int numB;  public:    /* Initialize numA and numB inside the MyClass      constructor using normal syntax. */    MyClass(int x, int y) {      numA = x;      numB = y;    }   // ... };

Assigning initial values to member variables numA and numB inside the constructor, as MyClass does, is a common approach, and is the way that member initialization is accomplished for many classes. However, this approach won't work in all cases. For example, if numA and numB were specified as const, like this,

class MyClass {   const int numA; // const member   const int numB; // const member

then they could not be given values by the MyClass constructor because const variables must be initialized and cannot be assigned values after the fact. Similar problems arise when using reference members, which must be initialized, and when using class members that don't have default constructors. To solve these types of problems, C++ supports a second member initialization syntax, which gives a class member an initial value when an object of the class is created.

The member initialization syntax is similar to that used to call a base class constructor. Here is the general form:

 constructor(arg-list) : member1(initializer),                           member2(initializer),                          // ...                         memberN(initializer) {  // body of constructor }

The members that you want to initialize are specified in a comma- separated list that goes after the constructor's declaration and before the body of the constructor. Notice the use and placement of the colon. You can mix calls to base class constructors with member initializations in the same list.

Here is MyClass rewritten so that numA and numB are const members that are given values using the member initialization syntax.

class MyClass {    const int numA; // const member   const int numB; // const member public:    // Initialize using initialization syntax.   MyClass(int x, int y) : numA(x), numB(y) { }    // ... };

Here, numA is initialized with the value passed in x, and numB is initialized with the value passed in y. Even though numA and numB are now const, they can be given initial values when a MyClass object is created because the member initialization syntax is used.

One last point: Class members are constructed and initialized in the order in which they are declared in a class, not in the order in which their initializers occur.

A virtual function is defined in a base class and overridden by a derived class. Virtual functions are how C++ supports polymorphism.

The explicit specifier applies only to constructors. Any time that you have a constructor that requires only one argument, you can use either ob(x) or ob = x to initialize an object. The reason for this is that whenever you create a constructor that takes one argument, you are also implicitly creating a conversion from the type of that argument to the type of the class. A constructor specified as explicit will be used only when an initialization uses the normal constructor syntax, ob(x). No automatic conversion will take place and ob = x will not be allowed. Thus, an explicit constructor creates a “nonconverting constructor.”




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net