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 {
public:
// 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 {
public:
const BigClass& val() const { return _val; }
BigClass& val(){ return _val; }
// ...
};

(可以根据有无const进行val()的重载;需要注意的是如果BigClass的引用前不加const,val()函数无法声明为const函数,因为_val的值可能通过引用在外面被改变,也就是说val()函数起不到const保护数据成员的目的)

8.class Triangular {
public:
bool next(int &val) const;
void next_reset() const { _next = _beg_pos - 1; }
// ...
private:
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; }

在其他地方定义也需要标注const关键词:

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修饰进行重载:

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
// ...
}

11.关于this指针:

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
tr1.copy(tr2);
is internally transformed into
// internal code transformation:
// tr1 becomes the class object addressed by the this pointer
copy(&tr1, tr2);

12静态成员函数

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):

13.typedef

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

14.关于友元函数

如果某个非类成员函数调用了某个类的私有成员,则需要在该类的声明中用friend关键字声明该函数为友元函数,如:

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

//必须在Triangular类定义之前定义Triangular_iterator类

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
function.

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不是很清楚。

猜你喜欢

转载自blog.csdn.net/zhuwj06/article/details/6213756
今日推荐