类成员函数(Class Member Functions)

A member function that is defined inside the class is implicitly treated as an inline function.

Member Functions Have an Extra, Implicit Parameter

class Sales_item {
public:
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
    { return isbn == rhs.isbn; }
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;
};

Each member function (except for static member functions) has an extra, implicit parameter named this . When a member function is called, the
this parameter is initialized with the address of the object on which the function was invoked. To understand a member function call, we might think that when we write total.same_isbn(trans); it is as if the compiler rewrites the call as

// pseudo-code illustration of how a call to a member function is translated
Sales_item::same_isbn(&total, trans);

We now can understand the role of the const that follows the parameter lists in the declarations of the Sales_item member functions: That const modifies the type of the implicit this parameter. When we call total.same_isbn(trans) , the implicit this parameter will be a const Sales_Item* that points to total . It is as if the body of same_isbn were written as

// pseudo-code illustration of how the implicit this pointer is used
// This code is illegal: We may not explicitly define the this pointer ourselves
// Note that this is a pointer to const because same_isbn is a const member
bool Sales_item::same_isbn(const Sales_item *const this,
const Sales_item &rhs) const
{ return (this->isbn == rhs.isbn); }

A const object or a pointer or reference to a const object may be used to call only const member functions. It is an error to try to call a nonconst member function on a const object or through a pointer or reference to a const object.

class Sales_item {
public:
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
    { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;
};

The colon and the following text up to the open curly is the constructor initializer list . A constructor initializer list specifies initial values for one or more data members of the class. We need not specify an initial value for the isbn member. Unless we say otherwise in the constructor initializer list, members that are of class type are automatically initialized by that class' default constructor. Hence, isbn is initialized by the string default constructor, meaning that isbn initially is the empty string. Had we needed to, we could have specified a default value for isbn in the initializer list as well.

The compiler-created default constructor is known as a synthesized default constructor . It initializes each member using the same rules as are applied for variable initializations. Members that are of class type, such as isbn , are initialized by using the default constructor of the member's own class. The initial value of members of built-in type depend on how the object is defined. If the object is defined at global scope (outside any function) or is a local static object, then these members will be initialized to 0. If the object is defined at local scope, these members are uninitialized.

posted on 2014-04-23 21:17  江在路上2  阅读(219)  评论(0)    收藏  举报