Thinking in C++读书笔记(三)---C++中的C

这章的名字虽然叫C++中的C,但是主体却是给我们介绍了很多C++中和C中原来不知道的只是,真心感觉C++/C的灵活和伟大。努力学习!

第三章 C++中的C

1 C++和C中的不同:

因为C++是向下兼容的,所以大部分的C C++是支持的,但是依然有一些差别。

⑴函数原型:int func(int x,int y,int z);

在C中参数是一定要命名的,但是在C++中参数可以不命名,这是为了给函数预留参数,将来修改时只需要给预留参数命名就可以使用,而不需要改变外部接口。

⑵C中int func();代表的意思是不确定的参数数目,而C++中代表没有参数的函数。C中没有参数的函数是int func(void);,C++中不确定参数用int func (...);

⑶C中变量的定义必须在块开始部分声明所有变量,而C++允许在作用于内的任意地方定义变量。原则是实时定义变量,什么时候用什么时候有定义。

⑷C中类型转换可以使用float a=float(100);这种形式进行隐式的转换。在C++中更好的习惯是使用显示的转换,使转换更安全。

    静态转换(static_cast):用于明确定义的转化,类似int=0x7fff;long l=static_cast<int>(i);

    常量转换(const_cast):用于const和非const之间的转换。

    重解释转换(reinterpret_cast):

#include <iostream>
using namespace std;

const int sz=100;
struct X {int a[sz];};

void print(X* x)
{
	for(int i=0;i<sz;i++)
	{
		cout<<x->a<<' ';
		cout<<endl<<"----------------------------------------"<<endl;
	}
}
int main()
{
	X x;
	print(&x);
	int* xp=reinterpret_cast<int*>(&x);
	for(int* i=xp;i<xp+sz;i++)
	{
		*i=0;
	}
	print(reinterpret_cast<X*>(xp));
	print(&x);
	return true;
}

reinterpret_cast通常是一种不明智的编程方式,但是当必须使用它时,它是非常有用的。

⑸C和C++中的枚举有所不同,C中不对枚举进行类型检查,而C++中需要进行严格的类型检查,将枚举名字当做是保留字进行检查。枚举可以让表达一件有多种特征的事物变得更清晰。(试图实现类的功能,C++中很少使用了)


2 switch语句:

switch选择器只允许使用整形,如果想要实现字符串选择器需要使用if-else。

3 内建类型:

标准C的内建类型(由C++继承)规范不说明每个内建类型必须有多少位,规范只规定内建类型必须能存储的最大值和最小值。系统文件limits.h和float.h中定义了不同数据类型可能存储的最大值和最小值。

 说明符:C中有4种内建类型,分别是int、char、float、double。C中又有四种说明符,long、short、signed、unsigned。四种内建类型和四种说明符可以组合搭配使用。

 这个程序是用来测试各种内建类型的大小的,同时也说明了哪些可以组合哪些不可以组合。

#include <iostream>
using namespace std;

int main()
{
	cout<<"short int:"<<sizeof(short int)<<endl;
	cout<<"int:"<<sizeof(int)<<endl;
	cout<<"long int:"<<sizeof(long int)<<endl;
	cout<<"signed int:"<<sizeof(signed int)<<endl;
	cout<<"unsigned int:"<<sizeof(unsigned int)<<endl;
        cout<<"********************************************************************"<<endl;
	//cout<<"short char"<<sizeof(short char)<<endl;//illegal
	cout<<"char:"<<sizeof(char)<<endl;
	//cout<<"long char"<<sizeof(long char)<<endl;//illegal
	cout<<"signed char:"<<sizeof(signed char)<<endl;
	cout<<"unsigned char:"<<sizeof(unsigned char)<<endl;
	cout<<"********************************************************************"<<endl;
	//cout<<"short float"<<sizeof(short float)<<endl;//illegal
	cout<<"float:"<<sizeof(float)<<endl;
	cout<<"long float:"<<sizeof(long float)<<endl;//warning: nonstandard extension used : long float
	cout<<"signed float:"<<sizeof(signed float)<<endl;//warning: 'signed' : can not be used with type 'float'
	cout<<"unsigned float:"<<sizeof(unsigned float)<<endl;//warning: 'signed' : can not be used with type 'float'
	cout<<"********************************************************************"<<endl;
	//cout<<"short double"<<sizeof(short double)<<endl;//illegal
	cout<<"double:"<<sizeof(double)<<endl;
	cout<<"long double:"<<sizeof(long double)<<endl;
	cout<<"signed double:"<<sizeof(signed double)<<endl;// warning: 'signed' : can not be used with type 'double'
	cout<<"unsigned double:"<<sizeof(unsigned double)<<endl;// warning: 'signed' : can not be used with type 'double'
	return true;
}

4 指针简介

非常有意思的一个小程序,能帮助理解程序的内存布局:

#include <iostream>
using namespace std;

int a,b,c;
char d,e;
void func1(int x,int y)
{
	cout<<x<<y<<endl;
}

void func2(int x,int y)
{
	cout<<x<<y<<endl;
}

void func3(int x,int y)
{
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
	cout<<x<<y<<endl;
}

void func4(int x,int y)
{
	cout<<x<<y<<endl;
}
int main()
{
	int i,j,k;
	int l=0;
	int m=99999999;
    char *n=(char*)malloc(sizeof(char)*10);
	char *o=(char*)malloc(sizeof(char)*30);
	cout<<"a:"<<(long)&a<<endl;
	cout<<"b:"<<(long)&b<<endl;
	cout<<"c:"<<(long)&c<<endl;
	cout<<"d:"<<(long)&d<<endl;
	cout<<"e:"<<(long)&e<<endl;
	cout<<"func1:"<<(long)&func1<<endl;
	cout<<"func2:"<<(long)&func2<<endl;
	cout<<"func3:"<<(long)&func3<<endl;
	cout<<"func4:"<<(long)&func4<<endl;
	cout<<"i:"<<(long)&i<<endl;
	cout<<"j:"<<(long)&j<<endl;
	cout<<"k:"<<(long)&k<<endl;
    cout<<"l:"<<(long)&l<<endl;
	cout<<"m:"<<(long)&m<<endl;
	cout<<"n:"<<(long)&n<<endl;
	cout<<"o:"<<(long)&o<<endl;
	return true;
}

5 传参的两种方式

⑴按值传递:

#include <iostream>
using namespace std;
//按值传递
void func(int x)
{
	cout<<x<<endl;
	x=80;
	cout<<x<<endl;
}

int main()
{
	int y=5;
	cout<<y<<endl;
	func(y);
	cout<<y<<endl;
	return true;
}
⑵引用传递:

#include <iostream>
using namespace std;
//按值传递
void func(int* x)
{
	cout<<*x<<endl;
	*x=80;
	cout<<*x<<endl;
}

int main()
{
	int y=5;
	cout<<y<<endl;
	func(&y);
	cout<<y<<endl;
	return true;
}
运行以上的两个程序就明白传值和引用的区别了。

6 void*:

它意味着任何类型的地址都可以间接的引用这个指针。

7变量的作用域:

变量的作用域由变量所在最近的一对括号确定。switch-case中不能定义变量。

8指定存储空间分配:

⑴全局变量:全局变量在所有的函数体外定义,程序的所有部分都可以使用(甚至文件外的代码),只要在文件中用extern声明另一个文件中存在的全局变量,那么这个文件就可以使用这个变量。

⑵局部变量:寄存器变量,关键字register告诉编译器“尽可能快的访问这个变量”。最好避免使用。

⑶静态变量:a 静态变量在真个程序声明周期都存在。而且static的初始化只在第一次调用时执行,函数调用之间变量的值保持不变。用这种方式,函数可以记住函数调用之间的一些信息片段。

                        b static变量的优点是在函数范围之外它是不可用的,所以它不可能被轻易的改变。这会使错误局部化。

#include <iostream>
using namespace std;

int func()
{
	static int x=0;
	cout<<x++<<endl;
	return true;
}
int main()
{
	for(int i=0;i<100;i++)
	{
		func();
	}
	return true;
}
⑷外部变量:为了理解外部变量就必须要说明一下连接器,连接器分为两种连接方式:内部连接和外部连接。内部连接时只对正在编译的文件创建一片存储空间,别的文件可以使用相同的标示符或全局变量,连接器不会发现冲突---可以用static指定。

 外部连接必须为被编译过得文件创建一片单独的存储空间,一旦创建连接器必须解决所有对这篇存储空间的引用。全局变量和函数名有外部连接,通过extern声明,可以从其他文件访问这些变量和函数。函数之外的所有变量(在C++中除了const)和函数定义默认为外部连接。可以使用static特地强调他们具有内部连接,也可以在定义时使用关键字extern显示的指定标示符具有外部连接。在C中,不必用extern定义变量或函数,但是在C++中对于const有时必须使用。

⑸常量:const定义常量,在C++中const只不过是一个标记,意思是“不要改变我”,必须初始化,后续详细。

9 宏定义简介:

#define PRINT(STR,VAR)\
  cout<<STR "=" <<VAR<<endl;
可以让输入更简单(避免输入错误);

10 位运算符:(速率最快,一次位操作相当于一个机器指令)

#include <iostream>
using namespace std;

void printBinary(const unsigned char);

#define PR(STR,VAR)\
	cout<<STR;printBinary(VAR);cout<<endl;

void printBinary(const unsigned char var)
{
	for(int i=7;i>=0;i--)
	{
		if(var & 1<<i)
		{
			cout<<1;
		}
		else
		{
			cout<<0;
		}
	}
}
int main()
{
	unsigned int getval;
	unsigned char a,b;
	cout<<"enter a number between 0 and 255:";
	cin>>getval;
	a=getval;
	PR("a in binary:",a);
	return true;
}
^异或:表示两者真值不同。

11 sizeof()-----独立运算符

      提供对于有关数据项目分配内存的大小。

12 asm允许C++调用汇编语言。

13 typedef:

     长的数据类型或者函数类型可以使用typedef进行重命名。

14 union:

当想用一个对象处理不同种数据类型时,可以使用union节省内存。

#include <iostream>
using namespace std;

union pack{
	char i;
	int j;
	short k;
};

int main()
{
	cout<<"sizeof(pack) ="<<sizeof(pack)<<endl;
	pack p;
	p.i='c';
	cout<<p.i<<endl;
	cout<<"sizeof(pack) ="<<sizeof(pack)<<endl;
	p.j=10;
	cout<<p.j<<endl;
	cout<<p.i<<endl;
	cout<<"sizeof(pack) ="<<sizeof(pack)<<endl;
}
去掉pack中的元素,看看union的大小变化。

15 数组的指针:(很有趣!)

#include <iostream>
using namespace std;

void func1(char* pArray1)
{
	cout<<"char*:"<<(long)pArray1<<endl;
}
void func2(char pArray2[])
{
	cout<<"char[]:"<<(long)pArray2<<endl;
}

int main()
{
	char array[3]={'a','b','c'};
	cout<<"array is:"<<(long)array<<endl;
	cout<<"&array is:"<<(long)&array<<endl;
	cout<<"&array[] is:"<<(long)&array[0]<<endl;
	cout<<"&array+1 is:"<<(long)((&array)+1)<<endl;
	cout<<"&array[0]+1 is:"<<(long)((&array[0])+1)<<endl;
	cout<<"array+1 is:"<<(long)(array+1)<<endl;
	func1(array);
	func2(&array[0]);
	system("pause");
	return true;
}
运行一下这个程序,仔细的会发现((&array)+1)和其他的地址不一样。这是因为&array的类型是char*[3],+1是加了一个该类型的长度,也就是3。

16 #

 在表达式之前加“#”可以把任何一个表达式转换成一个字符数组。

17 调试

     可以用#define DEBG

  #ifdefine DEBUG

    /*debug code*/

     #endif 的形式来进行程序的调试。

18 assert:

     cassert.h中定义的assert宏可以帮助我们调试程序,int i=100;assert(i!=100);断言为false,程序终止。通过在#include <cassert>前加入#define NDEBUG可以消除宏产生的代码。

19 复杂的声明和定义:

/*1*/void *(*(*fp1)(int))[10];
/*2*/float (*(*fp2)(int,int,float))(int);
/*3*/typedef double (*(*(*fp3)())[10])();
这些都是什么类型,自己试着看几遍就知道了。得仔细点!

20指向函数的指针数组:

一段程序说明问题:

#include <iostream>
#include <string>
using namespace std;

#define DF(N) void N(){ \
	cout<<"fucntiong "#N" called..."<<endl;}

DF(copy);DF(quit);
void (*func_table[])()={copy,quit};

int main()
{	
	cout<<"please input a command"<<endl;
	string strCmd;
	while(cin>>strCmd)
	{
		if(!strCmd.compare("copy"))
		{
			(*func_table[0])();
		}
		else if(!strCmd.compare("quit"))
		{
			break;
		}
		else
		{
			cout<<"command is invalid"<<endl;
		}
		cout<<"please input a command"<<endl;
	}
	return true;
}

21makefile----简化工作的工具:

project file其实也是一种Makefile。

我的博客里有一章专门讲makefile。









  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春阳CYang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值