a class use data abstraction and encapsulation defines an abstract data type.

  • the class designer worries about how the class is implemented.
    Programmers who use the class need not know how the type works. They can instead think abstractly about what the type does.

Basical Features

也分为declaration和definition。其中declare称为 forward declaration,出来的type叫 incomplete type。(在class表达完前,始终未完成definition,但是算是declare过了)

  • 可以用于不会真的用到class type实体的情况,比如:定义指针和引用;函数声明。(因此我们不能在class中用这个class,但可以指针,也就是链表)
  • Member functions 必须在内声明,定义随意;nonmember but interface 都在外面
  • funcions defined in the class are implicitly inline

Constructors

Access Specifiers

  • public: accessible to all parts of the program. define the interface
  • private: accessible to class. encapsulate (i.e., hide) the implementation.

(这里正式开始了class的介绍,因为struct就是…)

Friends

【引子】有些在class外面定义的函数,虽然不是mem func了,但是还是interface,所以还是要用data mem,那怎么办呢,就宣告这个函数是我的朋友!要写进class里

A class can allow another class (or its member function) or function to access its nonpublic members by making that class or function a friend.

  • Friend declarations may appear only inside a class definition.
  • 可以出现在class中的任何位置
  • 不是mem of the class
  • 不受access specifier影响

Friend Declaration is not Declaration

A friend declaration only specifies access. It is not a general declaration of the function. 也因此在认一个friend之前是可以不用先声明这个函数的

Class Scope

我们知道,正是每一个class都会定义它自己的scope,所以我们才只能通过obj及其指针和引用,来接触这些member。

这也是为什么当在class外定义memfunc时,要域解析一下。

但仅此而已吗?还有没有别的特性?

  • 【在class外定义memfunc时】由于函数名进行了域解析,这相当于提醒了编译器“这是在class内部”,所以之后的paraList和funcBody都不用再域解析了;
    但是显而易见的是,return type是发生在域解析之前的,所以return type如果需要class内的类型,那么还是需要域解析的~

Name Lookup

通常的顺序是

  • 看这个block
  • 看外面的block(但不会往后看
  • 报错

但是对于class中的memFunc,总是class‘ mem declaration先被编译,再是函数体被编译,所以总的来讲顺序是:

  • 看这个block
  • 看整个外面的class(也就是会往后看
  • 再往外看

也因为第一个特点,所以当memFunc不合理地使用了mem‘s name作为para’s name时,直接用这个名字实际上在用para而不是mem。要用外面的就用scope operator

Names are Resolved Where They Appear within a File

如果memFunc在class外被定义,那最后一步lookup是看这个定义所在的scope,而不是class所在的scope:

int height;  // defines a name subsequently used inside Screen
class Screen { 
public:
	typedef std::string::size_type pos;
	void setHeight(pos);
	pos height = 0; // hides the declaration of height in the outer scope 
}; 
Screen::pos verify(Screen::pos); 
void Screen::setHeight(pos var) {
	// var: refers to the parameter
	// height: refers to the class member
	// verify: refers to the global function
	height = verify(var);
}

注意看,讲道理,在class被完全定义之前,verify并未被声明,但是结果在函数体中,使用verify是合法的。这就说明了 the name is resolved in the function body’s scope, rather than the class scope.

p285, 不懂,Type Names Are Special

static Class Members

Literal Classes


Additional Class Features

- Defining a Type Member

就是在class里的 typedefusing

- mutable Data Members

用于给const obj但是想要改变的data mem

mutable int access_ctr;
  • A mutable data member is never const, even when it is a member of a const object.
termsmeans
data abstractiona technique that separate interface and implementation.
encapsulationseparate a class’ interface and implementation.
name lookupthe process of finding which declarations match the use of a name