• C++ 布尔 const 引用 默认参


    day02

    1.布尔

    C语言使用布尔类型的方法如下

    //利用宏
    #include<stdio.h>
    typedef int BOOL;
    #define TRUE 1
    #define	FALSE 0
    int main()
    {
    	BOOL bVal = TRUE;
    	bVal = FALSE;
    
    	bVal = 8;//可读性差
    
    }
    

    c++中直接使用 bool,如下

    //利用关键字
    int main()
    {
    	bool bVal = true;
    	bVal = false;
        
        bVal =2;//非零为真。。内存为  01
        //bool只有一个字节
        
        
    	
    }
    

    2.const

    相比于C语言优化了无参宏。

    宏缺点:1)没有类型检查 2)没有符号(无法调试)

    const int VAL = 10000;//定义了一个常量
    
    int main()
    {
    	char ch = VAL;
    	cout << ch << endl;
    	*(int*)&VAL = 10;//不能更改,发在只读数据区
    	const int val = 1000;
    	*(int*)&val = 10;
    	cout << val << endl;//const 修饰的是常量,在编译过程中会进行替换,将val替换成1000
    	return 0;
    }
    
    //修饰指针防止误操作
    void ShowArray(const int* pAry, int nCnt)
    {
    	//pAry[2] = 5566; 
    	*(int*)(pAry + 2) = 5566;//不能修改是语法层的限制
    	for (int i = 0; i < nCnt; i++)
    	{
    		cout << pAry[i] << " ";
    	}
    	cout << endl;
    }
    int main()
    {
    	int ary[] = { 12,312,312,123,41,231 };
    	ShowArray(ary, sizeof(ary) / sizeof(ary[0]));
    	return 0;
    }
    

    const tpye* p

     // const tpye* p ; p指向的内容不可以被修改,p本身可以被修改	
    	int n = 1;
    	int n2 = 3;
    	const int* p = &n;
    	p = &n2;
    	//*p = 8;
    

    tpye const* p

    // tpye	const * p ; p指向的内容不可以被修改,p本身可以被修改	
    	int n = 1;
    	int n2 = 3;
    	int const* p = &n;
    	p = &n2;
    	//*p = 8;
    

    tpye* const p

     // const tpye* p ;p指向的内容可以被修改,p本身不可以修改。
    	int n = 1;
    	int n2 = 3;
    	int* const p = &n;
    	//p = &n2;
    	*p = 8;
    

    const tpye* const p

    //const tpye* const p ;p指向的内容不可以被修改,p本身不可以被修改
    	int n = 1;
    	int n2 = 3;
    	int const* const p = &n;
    	p = &n2;
    	*p = 8;
    

    eg:

    	int nVal1 = 8;
    	int nVal2 = 9;
    
    	const int* p1 = &nVal1;
    	int const* p2 = &nVal2;
    
    	const int* const p3 = &nVal1;
    	const int* const p4 = p1;
    	const int* const p5 = p2;
    
    	//int* p6 = p1;//int*与const int*类型不匹配
    	//int* p7 = p3;//int*与const int*类型不匹配
    	const int* p8 = p3;
    	//int* const p9 = p3;//int*与const int*类型不匹配
    	//int* const p10 = p1;//int*与const int*类型不匹配
    

    3.引用

    指针的危害

    //可能误用到野指针,。空指针
    void Swap(int* pn1, int* pn2)
    {
    	int nTmp = *pn1;
    	*pn1 = *pn2;
    	*pn2 = nTmp;
    }
    int main()
    {
    	int n1 = 2;
    	int n2 = 3;
        //Swap(nullptr,&n2);//报错。访问权限
    	int* p = (int*)malloc(4);
    	free(p);
    	//Swap(p, &n2); 使用了野指针。
    }
    

    使用引用的两数交换

    void SwapXXXX(int& nRef1, int& nRef2)
    {
    	int nTmp = nRef1;
    	nRef1 = nRef2;
    	nRef2 = nTmp;
    }
    int main()
    {
    	int n1 = 2;
    	int n2 = 3;
    
    	SwapXXXX(n1, n2);
    }
    

    引用的基础了解

    int main()
    {
    	//引用:起别名
    	int n1 = 2;
    	int& n1Ref = n1;
    	int& n1Ref2 = n1;
    	n1Ref = 5;
    	n1Ref2 = 2;
    }
    

    引用与指针的区别

    int main()
    {
    	//引用与指针的区别
    	//int& nRef;//报错,引用必须初始化
    	//int n = 9;
    	//int& nR1 = n;
    	//int&& nR2 = nR1;//引用没有多级引用
    	//int n1 = 8;
    	//nR1 = n1;//引用不能改变引用的变量,这里其实是赋值语句
    	//int& n = 9;//应用不能引用常量
    	const int& n = 9;//const修饰的引用可以引用常量
    
    }
    

    引用适用于所有类型

    #if 0
    int& func()
    {
    	int n = 9;
    	return n;
    }
    int main()
    {
    	//int& nRef1 = func();//不要返回局部变量的引用,因为局部变量当函数使用完后会释放掉
    	//引用可以用于所有类型
    	float f = 3.21f;
    	float& fR = f;
    	fR = 8.6f;
    	char ch = 'a';
    	char& chR = ch;
    	chR = 'b';
    	struct tagTest
    	{
    		int m_n;
    		float m_f;
    	};
    	tagTest t;
    	tagTest& tR = t;
    	t.m_f = 6.54f;
    	t.m_n = 12;
    
    
    	int n4 = 9;
    	int* pn4 = &n4;
    	int*& pn4R = pn4;
    	int n5 = 98;
    	pn4R = &n5;
    	*pn4R = 69;
    }
    
    

    引用的本质还是指针

    //通过调试查看堆栈可知道引用的本质还是指针
    void SwapXXXX(int& nRef1, int& nRef2)
    {
    	int nTmp = nRef1;
    	nRef1 = nRef2;
    	nRef2 = nTmp;
    }
    int main()
    {
    	int n1 = 2;
    	int n2 = 4;
    	SwapXXXX(n1, n2);//引用本质还是指针
    }
    
    

    4.默认参

    1.当函数调用没有提供实参的时候,会使用默认参
    2.如果提供了实参,则会使用实参。
    3.默认参必须从右向左依次设置,中间不能间断
    4.默认参要放到声明里面,不能同时放到声明和实现中。

    使用宏定义

    void Say(const char* szUser, const char* szCon)
    {
        cout << szCon << "打招呼" << szCon << endl;
    
    }
    #define SAY(szUser) Say(szUser,"你好")
    int main()
    {
    
        SAY("安安");//放在常量数据区,不可被修改
      
        Say("啊瑟", "你好");
    
    }
    
    

    使用默认参数

    void Say(const char* szUser, const char* szCon="你好")
    {
        cout << szCon << "打招呼" << szCon << endl;
    
    }
    int main()
    {
    
        Say("莫再提");
        Say("莫再提");
        Say("莫再提");
        Say("西门吹雪", "吃了");
    
        std::cout << "Hello World!
    ";
    }
    

    注意:一般在.h文件中定义函数声明。

  • 相关阅读:
    创业失败的七个原因及解决之道
    技术人员如何参与产品设计讨论:激活那一潭死水
    基于Android Studio搭建hello world工程
    WINCE6.0+IMX515通过cfimager.exe烧录镜像文件
    基于Android Studio搭建Android应用开发环境
    WinCE启动失败的原因与解决办法分析
    Maximum Allowed Error 7 错误解决
    s3c6410 开发板Linux系统支持 K9GAG08U0E的方法
    Nandflash 驱动移植
    GIFT-EMS礼记----青软S2SH(笔记)
  • 原文地址:https://www.cnblogs.com/pupububu/p/14160693.html
Copyright © 2020-2023  润新知