O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Inline Implementation

Just as you can ask the compiler to make a regular function inline, you can make class methods inline. The keyword inline appears before the return value. The inline implementation of the GetWeight() function, for example, looks like this:

inline int Cat::GetWeight()
{
    return itsWeight;         // return the Weight data member
}

You can also put the definition of a function into the declaration of the class, which automatically makes that function inline. For example:

class Cat
{
public:
    int GetWeight() const { return itsWeight; }     // inline
    void SetWeight(int aWeight);
};

Note the syntax of the GetWeight() definition. The body of the inline function begins immediately after the declaration of the class method; there is no semicolon ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required