1. 规矩
变量名 g_a ----代表全局变量
g_t ----代表他是一个typedef类型
把程序中的警告要全部去掉,有警告是不合格的代码
#define MAX 10 //这个时候要求全部大写
2. 引用
引用int &re =a //这个时候&re不是代表地址,而是代表别名。 引用通常会写成re_xxx;
- 引用的时候一定要初始化。
- 引用就是一个常量指针
- 引用不会在栈上开空间
引用可以作为函数的返回值,同理指针
1)缺点:返回引用的时候,返回的引用不能是局部变量的引用,会报警而且局部变量销毁后,得到的数值会很奇怪
2)缺点:返回引用的时候,返回的引用如果是静态局部变量的引用,会报警而且局部变量销毁后,得到的数值会是正确的,但是不推荐这么用
3)缺点:返回引用的时候,返回的引用如果是在堆中调用的局部变量的引用,会报警,得到的数值会是正确的,但是不推荐这么用
4)好处: 如果引用当作函数返回值的话,函数可以作为左值:但是返回的变量不能是局部变量
3. 读写速度
网盘最慢,硬盘其次,内存相对快,缓存最快,CPU(寄存器)最快
4. inline函数
宏函数,快,缺点是没有语法检测的能力(因为它是在编译之前做的) ---预处理进行
普通函数,缺点慢(因为需要压栈,出栈) ----编译器进行
解决宏函数的语法检测,又能解决普通函数处理慢的问题,就出现了inline函数
inline函数不需要压栈和出栈,但是是在编译器里面进行,牺牲了代码的空间
总结:
1)函数比较简单,而且需要频繁使用,就可以用inline函数
2)如果代码比较复杂, 或者代码比较长,即使写成inline函数,编译器器也会按照普通代码来处理(编译器会权衡)
5. 默认参数,占位参数----典型bug,没有用
//int sum(int a=100, int b)不可以这样写,默认参数一定是从后往前
int sum(int b, int a = 100)
{
return a + b;
}
int main()
{
int c = 200, d = 20;
cout << sum(c);//默认c=b, a=100;
return 0;
}
6. 函数重载和函数指针
函数重载, 如果函数名相同,但是函数参数不同,在c++中叫做函数重载
- 函数的返回值,函数的形参列表(参数个数,参数类型,参数的顺序)
- 函数重载,函数名相同,参数列表不同
- 返回值和参数重载没有任何关系
- 有匹配的函数就会优先选择完全匹配的,没有的话,会选择可以隐式转换的,比如 char->int, float->double
函数指针
- 函数指针最重要的作用是作为回收函数的一个形参
- 函数指针做重载的时候,隐式转换不可以用了,必须完全匹配才可以
typedef int (*P_SUM_t)(int, int, int);
typedef int (*P_SUM1_t)(int, int);
using namespace std;
int sum(int a, int b, int c)
{
return a + b + c;
}
int sum(int a, int b)
{
return a + b;
}
int main()
{
P_SUM_t p = sum;
cout << p(1, 2, 22) << endl;
P_SUM1_t p1 = sum;
cout << p1(1, 2) << endl;
return 0;
}
7. 运算符重载
- 重载运算符允许吧标准运算符(例如 +,-,*,/应用于自定义的数据类型的对象
- 不要滥用重载,目的是更容易写,更容易读,否则就没必要使用
例子一
complex.h
#ifndef _complex_h_
#define _complex_h_
#include<iostream>
using namespace std;
class complex
{
private:
char* name;
int age;
public:
complex(char* name_1, int age_1)//构造函数
{
name = name_1;
age = age_1;
}
complex();//构造函数复制
~complex();
//友元函数
friend ostream& operator<<(ostream& o, complex student);
friend istream& operator>>(istream& in, complex& student);
};
complex::complex() {
char n[] = "Elaine";
name = n;
age = 10;
}
complex::~complex() {
}
ostream& operator<<(ostream& o, complex student) {
cout << student.name << " " << student.age << endl;
return o;
}
istream& operator>>(istream& in, complex& student) {
in >> student.name >> student.age;
return in;
}
#endif /*_complex_h_*/
main.cpp
#include<math.h>
#include<vector>
#include<stack>
#include<algorithm>
#include<bitset>
#include<numeric>
#include<map>
#include<stdio.h>
#include "complex.h"
using namespace std;
int main()
{
char name_2[] = "Elaine_1";
int age_2 = 20;
complex stu(name_2, age_2);
cin >> stu;
cout << stu;
return 0;
}
例子二
opeartor.h
#ifndef _opeartor_h_
#define _opeartor_h_
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(double& x_, double& y_)
{
x = x_;
y = y_;
}
Point();
~Point();
double& operator[](int i);
Point operator+ (Point& point_C);
friend ostream& operator<<(ostream& out, Point point_A);
friend istream& operator>>(istream& in, Point& point_A);
};
Point::Point()
{
x = 20.1, y = 15.4;
}
Point::~Point()
{
}
double& Point::operator[](int i)
{
if (i == 0) return x;
else if (i == 1) return y;
else throw "invalid opeartion!!";//抛出异常
}
Point Point::operator+ (Point& point_C)
{
Point Z;
Z[0] = this->x + point_C[0], Z[1] = this->y + point_C[1];
return Z;
}
istream& operator>>(istream& in, Point& point_A)
{
cin >> point_A.x >> point_A.y;
return in;
}
ostream& operator<<(ostream& out, Point point_A)
{
cout << point_A.x << " " << point_A.y;
return out;
}
#endif /*_opeartor_h_*/
main.cpp
#include<iostream>
#include<string>
#include<string.h>
#include<iomanip>
#include<math.h>
#include<vector>
#include<stack>
#include<algorithm>
#include<bitset>
#include<numeric>
#include<map>
#include<stdio.h>
#include "operator.h"
using namespace std;
int main()
{
double c, d;
double e, f;
Point A(c, d);
Point B(e, f);
cin >> A;
cout << A << endl;
cout << A[0] << " " << A[1] << endl;//A.operator[0],A.operator[1]
A[0] = 23.0, A[1] = 25.0;//A.operator[0],A.operator[1]
cout << A[0] << " " << A[1] << endl;//A.operator[0],A.operator[1]
cin >> B;
cout << A + B << endl;
return 0;
}