Skip to content

1st chapter: Compiling

AntonioLMH edited this page May 31, 2019 · 3 revisions

分号加括号:结构体和类

.cpp->.ii->.s->.o->.out .ii: pre-compiling .ii->.s:编译器(ccl) .s->.o:汇编(as) .o->.out:链接(ld) (编译时只针对当前文件,所以编译通过)

declaration一定也要有definition.

Declaration: 1.extern variable 2.fundation prototypes 3.class/struct declaration

-D+宏名:编译所有的都带宏

基本语法: 1.冒号: 1.表示变量占几个空间 2.在构造函数后,表示类给成员变量赋值的方法 3.public和private后面表示定义的成员是 公开还是私有 4.类名后面定义类的继承

2.双冒号: 1.表示域操作符:比如类A里声明了f函数,但没有在类里给出f定义,所以在类外定义 void A::f().成员函数。 2.全局函数. 3.表示引用成员函数及变量. System::Math::Sqrt()相当于 Systen.Math.Sqrt()

成员变量不在类里面,在对象里面。 parameters和local variable属性一样(都在Stack中)

a.f()是属于class A的f函数,不是对象a的. this: the hidden parameter(pointer to the caller).

C++分配空间但不会把空间里的东西清0.

constructor: 构造对象时初始化。1.名字和类的名字是相同的 2.没有任何返回类型 {Tree t(12)}:打括号表示范围,在这个范围内构造t(把12传进构造函数)。 eg. Tree(int i){...} Tree t(12)

destructor: 对象被消灭的时候调用。没有返回类型和参数。{}是析构的范围。 eg. class Y{ public: ~Y(); };

define进入函数的时候就定义了,构造是运行到那一行在构造。

-sizeof c/ sizeof *c求数组长度

struct Y {float f; int i;Y(int a);}; eg. Y y1[]={Y(1),Y(2),Y(3)};

Default constructor 只要是无参数的都叫这个

new:相当于malloc(先调用)+构造,结果是个地址。 delete:相当于析构(先被调用)+free new [] 对应delete (eg. 创建时:int*psome = new int [10]; 释放: delete []psome;) new 是在操作系统给C++默认分配的空间里再分配(entry table会记录),不是再向操作系统要内存。

Clone this wiki locally