Essential C++笔记(基于对象的编程风格)

1.Private members can be accessed only by the member functions and friends of the class


2.As with the definition of a nonmember inline function, an inline member function should be placed in a header file. The class definition and the inline member functions are typically placed in a header file given the name of the class.


3.Triangular t5; //调用default constructor;t5后面无需打括号(和build-in type data类似)。如果加上圆括号,compiler会将t5解释为一个返回Triangle object 的函数(这是因为c++需要保持对c的兼容引起的)。


4.When we design a class, we must ask ourselves whether the default memberwise behavior is adequate for the class. If it is, we need not provide an explicit copy constructor. If it is not, we must define an explicit instance and within it implement the correct initialization semantics.(对于含有指针数据成员的类执行对象拷贝的时候尤其要小心,一不小心会造成指针错误)


5.class Triangular {
// const member functions
int length() const { return _length; }
int beg_pos() const { return _beg_pos; }


6.A const member function defined outside the class body must specify the const modifier in both its declaration and definition. For example,
int Triangular::elem(int pos) const
{ return _elems[pos-1]; }


7.class val_class {
const BigClass& val() const { return _val; }
BigClass& val(){ return _val; }
// ...



8.class Triangular {
bool next(int &val) const;
void next_reset() const { _next = _beg_pos - 1; }
// ...
mutable int _next,
int _beg_pos;
int _length;

用mutable修饰了之后,对_next所做的修改并不影响class object的常数性。



9.关于const成员函数:The class designer must tell the compiler by labeling as const each member function that does not modify the class object:

such as: int length() const { return _length; }


The const modifier follows the parameter list of the function. A const member function defined outside the class body must specify the const modifier in both its declaration and definition.



10.const成员函数如果返回非const的引用相当于将这个引用公开,存在风险(虽然程序在编译的时候不会出问题)Returning a non-const reference to _val in effect opens it to modification in the general.



const BigClass& val() const { return _val; }
BigClass& val(){ return _val; }

For a non-const class object, the non-const instance of val() is invoked. For a const class object, the const instance is invoked.例如:

void example(const BigClass *pbc, BigClass &rbc)
pbc->val(); // invokes const instance
rbc.val(); // invokes non-const instance
// ...



Internally, the compiler adds the this pointer as an argument to each class member function.
copy(), for example, is transformed as follows:
// Pseudo Code: Internal Member Function Transformation
Triangular& Triangular::
copy(Triangular *this, const Triangular &rhs)
this->_length = rhs._length;
this->_beg_pos = rhs._beg.pos;
this->_next = rhs._beg_pos-1;


This transformation requires a second transformation: Each invocation of copy() must now provide two arguments. To accomplish this, our original invocation
is internally transformed into
// internal code transformation:
// tr1 becomes the class object addressed by the this pointer
copy(&tr1, tr2);




A member function can be declared as static only if it does not access any nonstatic class members.

When defined outside the class body, the static keyword is not repeated (this is also true of static data  members):



A typedef introduces an alternative name for a type, and takes this general form:
typedef existing_type new_name;




class Triangular {
friend int operator*(const Triangular_iterator &rhs);
// ...
class Triangular_iterator {
friend int operator*(const Triangular_iterator &rhs);
// ...



The declaration can appear anywhere in the class definition. It is not affected by the private or
public access levels of the class.



Alternatively, we can grant friendship to the class itself, in turn conferring friendship on all the
member functions of that class.(此时不需要先定义Triangular_iterator类)


Friendship is generally required for performance reasons



15.关于function objects:

定义:A function object is an instance of a class that provides
an overloaded instance of the function call operator.


Overloading the call operator allows a function object to be used just as if it were a function.


为什么用function object?

A function object implements what we would otherwise define as an independent function. Why
do we bother? The primary reason is efficiency. We can inline the call operator, thereby
eliminating the function call overhead that comes with invoking the operation through a pointer to


in lt(ival), If lt is a class object, the compiler internally transforms the statement as follows:
lt.operator()(ival); // internal transformation


16.pointer to member function不是很清楚。

想对作者说点什么? 我来说一句