-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CC #61
Comments
reference引用
int x = 10;
int& y = x; // int& 是类型, 即指向int类型的引用 x,y 所指向的值 / x和y的地址都相同, 即 引用与const指针类似
const 引用
返回值为引用类型需要注意: 应避免返回在函数终止时在内存单元中不存在的内存单元引用. 例如 const awesome_struct& awesome_fn(const awesome_struct& arg){
awesome_struct tmp = arg;
return arg
} 在awesome_fn返回时, tmp变量就被销毁了, 返回tmp作为引用就存在问题(什么问题...空引用?), 同样应避免返回指向临时变量的指针. 最简单的是使用传入参数arg来作为返回值. _为何在返回值前加了 |
namespace好像没什么好说的... using declarationnamespace awesome_ns{
int variable = 10;
}
void fn(){
using awesome_ns::variable;
variable = 1
::variable = 10;
} 上面 using 是一个 using 声明, 将 using 编译指令using namespace awesome_ns;
int main(){
int a = variable;
} using编译指令, 将整个 namespace 中所有的名称都可用. |
template / generictemplate <T> T add(T a, T b){
return a + b;
}
int a = 1, b = 2;
add(a, b); // 直接调用 有时 specialization & instatiationspecializationspecial即特别化, 书中称 假设有一个awesome_type, 它的加法我们只想将 template <> awesome_type add<awesome_type>(awesome_type a, awesome_type b){
awesome_type ret;
ret.data = a.data + b.data;
return ret;
} 有了上面的代码之后, 再使用加法则会使用这个具体化之后的形式, 具体化, 我们加了awesome_type类型吗: awesome_type a = { .data = 1 }, b = { .data = 2 };
add(a,b); // 回去调用具体化加了awesome_fn类型之后的 cpp中函数可以重载, 函数的优先级为:
instantiationinstance实例, 说的是代码里面写个 int a = 1, b = 2;
add(a, b);
方法一 // explicit instantiation
template int add(int, int);
int a = 1, b = 2;
add(a, b); 这里使用 方法二 int a = 1, b = 2;
add<int>(a, b); 显示实例化
|
string & old char* in c |
class
class Demo{
private:
int data;
public:
int getData() { return data; }
}; 成员
构造函数
class Person{
private:
int m_age;
std::string m_name;
public:
int getAge(){ return m_age; }
std::string getName{ return m_name; }
// constructor
Person(){ /* default */ }
Person(int& age, std::string& name)
}; 调用
析构函数
Person::~Person(){
std::cout << "Bye" << std::endl;
} this指针前面 age/name 其实都是 函数最后的constclass Person{
public:
int getAge() const { return this->age; }
};
const方法表示不会干坏事:
enum classc中enum不用限定符, 可能经常发生冲突 enum Size { small, large, xlarge };
enum ShipSize { large, extraLarge }; 两个enum中有重复的, 编译不通过, 可以使用 enum class Size { small, large, xlarge };
enum class ShipSize { large, extraLarge };
// 使用class机制引用
Size::large
ShipSize::large |
上面的是C++ primier第10章的clas内容, clas内容较多, 分章节写笔记 class in ch11运算符重载函数名 拿个例子来说, 让Person类重载 Person::Person(int age){
this->age = age;
}
Person Person::operator+ (int age){
this->age += age;
return *this;
}
Person p = Person(18);
p = p + 1; // 加上 int, 自动增加年龄 友元
让函数成为类的友元, 可以赋予该函数与类的成员函数有相同的访问权限. 上述重载例子, 是 #include <iostream>
using namespace std;
class Person{
public:
// member
int age;
// constructor
Person(int age){ this->age = age; }
// operator+
Person operator+(int age) {
this->age += age;
return *this;
}
// 1 + p
friend Person operator+(int age, Person& p);
};
// 友元
Person operator+(int age, Person& p){
p.age += age;
return p;
}
int main(){
Person p(18);
p = 1 + p;
cout << p.age << endl;
return 0;
}
常用的友元, 重载
|
ch12 类和动态内存分配
复制构造函数class T{
private:
int num;
public:
T(int num){ this->num = num; }
};
T b = T(1);
T b(a); // form1
T b = T(a); // form2
T b = a; // form3
T* pb = new T(a); // form4 这四种情况下, 同
调用 赋值运算符重载
|
ch13 |
ch14 |
ch15 |
ch16stringconstructor
运算符重载string重载了 find
size() / length()都是返回 与
|
method | explanation |
---|---|
push_back / push_front |
push / unshift |
begin / end |
获取头尾 iterator |
erase(start_iterator, end_iterator) |
抹除, 包含 start, 不包含 end |
insert(start_iterator, 要被插入的iterator开始, 要被插入的iterator结束) |
... |
sort(start, end, compare_fn) |
排序 |
CC = c plus plus
The text was updated successfully, but these errors were encountered: