c++ 指针、引用、内存管理、函数(引用、重载、内敛)——c++复习(一)
因为最近写cocos2dx游戏,发现c++基本还给老师了,所以有必要再复习一下,温故希望能知新。废话不多说,直接码起来
#include <iostream>
typedef struct{
int red;
int blue;
}Color;
//函数声明
int getMaxOrMin(int*arr,int count,bool isMax);
void swapFun(int* a, int* b);
void swapFun2(int& a, int& b);
void func(const int &a, const int &b);
void func2(int i,int j=5,int k=10);
void fun3(int x,int y=10,int z=30);
void fun3(double x,double y);
int main(int argc, const char * argv[]) {
// insert code here...
#pragma mark 基础
#if 0
std::cout << "Hello, World!\n";
//1.c++与c的区别之bool类型:c语言非0为真,0为假; c++y有一种类型叫做bool,true为真,false为假
bool flag = 0;
if (flag) {
std::cout<<"真"<<std::endl;
}else{
std::cout<<"假"<<std::endl;
}
//2.c++初始化方法
//1):
int x= 1024;
//2):
int y(1024);//新的
//3.输出
bool z=0;
std::cin>>z;
std::cout<<z<<std::endl;
std::cout<<std::oct<<z<<std::endl; // oct 8进制
std::cout<<std::dec<<z<<std::endl; // dec 10进制
std::cout<<std::hex<<z<<std::endl; // hex 16进制
//4.c++命名空间:namespace
//5.实例
int a[7] = {1,2,3,0,3,2,9};
int result = getMaxOrMin(a, 7, true);
std::cout<<"result:"<<result<<std::endl;
#endif
#pragma mark 重点
#if 0
//6.引用(别名)——所以引用一定要有原数据
int b = 3;
int &c = b;//引用必须初始化(要有原数据), int &c;int &d = NULL; 这些语句是错误的
c = 10;
std::cout<<b<<std::endl;//对引用进行修改就是对原数据进行修改
//对结构体引用
Color c1;
Color &c2 = c1;
c1.red = 100;
c2.blue = 200;
std::cout<<c1.red<<":"<<c2.blue<<std::endl;
//指针引用**** —— 类型 *&指针别名 = 指针
int e = 10;
int * p = &e;//&取地址符
int *&q = p;
*q = 20;
std::cout<<e<<std::endl;
//引用作函数参数
int x = 10, y = 20;
swapFun(&x, &y);
std::cout<<x<<":"<<y<<std::endl;
swapFun2(x,y);
std::cout<<x<<":"<<y<<std::endl;
//7.const关键字,控制变量是否可以变化
const int x=3;//存储内容不可以改变,永远是3
const int* p1 = NULL;
int const * p2 = NULL;//p1和p2是等价的
int * const p3 = NULL;
const int * const p4 = NULL;
int const * const p5 = NULL;//p4和p5是等价的
//const与引用
const int &y = x; //这样就不可以通过y修改x
//
const int x2 = 3;
//int * y = &x; 用可变的指针指向不可变的变量是不允许的
int x3 = 3;
const int * y2 = &x3;//用不可变指针指向可变的变量是可以的
//函数引用
int a = 30,b=20;
std::cout<<a<<":"<<b<<std::endl;
func(a, b);
std::cout<<a<<":"<<b<<std::endl;
//8.函数,函数参数默认值:一定要把参数列表的默认值写在最右边fun(int i,int j=0) j有默认值
func2(10);
func2(20, 30);
func2(40, 50, 60);
//函数重载:在相同作用域下,用同一个函数名定义的多个函数,参数个数或参数类型不同,叫做函数重载(注意只有返回值不同是不可以的)
//在函数调用的时候,计算机使用函数名加参数,函数调用过程中是以函数参数类型优先的
fun3(1.1,1.2);
fun3(1,1);
//内敛函数 inline:在编译的时候,将函数代码直接替换掉调用代码,运行效率会有所提升;但是递归函数无法使用内敛方式,还是会使用普通函数的调用方式
#endif
//9 内存管理 申请内存new 释放内存delete
int * p = new int;//new int(20),这样的话,*p的初始化就为20
if (NULL == p) {
system("pause");
return 0;
//分配内存失败
}
delete p;
p = NULL;//避免成为野指针,如果不置为NULL;当后面再次调用的时候,会使得同一块内存被重复回收,就会出现异常
int * arr = new int[10];//块内存
delete []arr;//要以释放块内存的方式释放,如果delete arr,那么这样就只是释放了数组第一个元素的内存
arr = NULL;
/**
* c语言申请内存 malloc(size_t size),释放内存 free(void* memblock),c++与c语言不能混搭
*/
return 0;
}
/**
* 使用一个函数找出一个整形数组中的最大值盒最小值
*/
int getMaxOrMin(int*arr,int count,bool isMax) {
int temp;
if (count>0) {
temp = arr[0];
for (int i=1; i<count; i++) {
if (isMax) {
if (temp<arr[i]) {
temp = arr[i];
}
}else{
if (temp>arr[i]) {
temp = arr[i];
}
}
}
}
return temp;
}
/**
* 交换两个数的值,传址
*/
void swapFun(int* a, int* b) {
int temp = 0;
temp = *a;
*a = *b;
*b = temp;
}
/**
* 交换两个数的值,用别名
*/
void swapFun2(int& a, int& b) {
int temp = 0;
temp = a;
a = b;
b = temp;
}
/**
* func(int &a, int &b)这说明形参a,b是对实参的引用,在函数里对a,b的修改也是对实参,
* func(const int &a, const int &b) 如果是这样,那在函数里对形参进行修改是会报错的
*/
void func(const int &a, const int &b) {
// a = 10;
// b = 20;
std::cout<<a<<":"<<b<<std::endl;
}
//函数
void func2(int i,int j,int k) {
std::cout<<i <<j <<k <<std::endl;
}
//重载
/**
* 在函数调用的时候,计算机使用函数名加参数——getMax_int_int_int;getMax_double_double_double的命名方式形成一个新的函数
*/
void fun3(int x,int y,int z)
{
std::cout<<x << y <<z <<std::endl;
}
void fun3(double x,double y)
{
std::cout<<x << y <<std::endl;
}
posted on 2016-04-22 14:53 hello_vzjw 阅读(111) 评论(0) 收藏 举报
浙公网安备 33010602011771号