当前位置: 首页 > news >正文

中山做网站比较好免费b站推广网站详情

中山做网站比较好,免费b站推广网站详情,国内网站建设网站排名,有哪些简单的网站条款2 常量(const)替换宏(#define)指针常量类成员常量 枚举(enum)替换宏(#define)模板函数(template inline)替换宏函数 尽量用const,enum,inline替换#define 总结就是: 常量(const)替换宏(#define) // uppercase names are usually for macros #define ASPECT_R…

条款2

  • 常量(const)替换宏(#define)
    • 指针常量
    • 类成员常量
  • 枚举(enum)替换宏(#define)
  • 模板函数(template inline)替换宏函数

尽量用const,enum,inline替换#define

在这里插入图片描述
总结就是:
在这里插入图片描述

常量(const)替换宏(#define)

// uppercase names are usually for macros
#define ASPECT_RATIO 1.653
const double AspectRatio = 1.653;

指针常量

双const:指针不能变(只能指向这一块内存空间),指向的内存单元的值也不能变。

	std::cout << "测试常量指针" << std::endl;const char* const authorName1 = "StevePro Studios";   //c类型字符串 char*   std::cout << *(authorName1 + 0) << *(authorName1 + 1) << std::endl;char* const authorName12 = "StevePro Studios2";authorName12[2] = 'x';  //指针只能指向S的地址(值可变)  authorName12[2] =nullptr;不可修改const char* authorName13 = "StevePro Studios3";authorName13 = nullptr; //指针可以指向其他内存空间,但内存空间中的值不能变。  authorName13[2] = 'x';不可修改

或者可以使用string常量代替:

	std::cout << "string常量" << std::endl;const std::string authorName2("StevePro Studios");    //c类型字符串 string

在这里插入图片描述

类成员常量

  1. 类的静态成员
  2. 在头文件声明,在cpp文件中定义

在这里插入图片描述
在这里插入图片描述
这样也就完成了常量的定义。

	std::cout << GamePlayer1::NumTurns << std::endl;GamePlayer2 g2;std::cout << g2.scores << std::endl;

可以在其他函数中,直接调用常量GamePlayer1::NumTurns。
在这里插入图片描述

枚举(enum)替换宏(#define)

在这里插入图片描述
在这里插入图片描述

也可以在其他函数中直接使用常量:

	GamePlayer2 g3;std::cout << g3.NumTurns << std::endl;

模板函数(template inline)替换宏函数

#include <iostream>#include "GamePlayer1.h"
#include "GamePlayer2.h"
// uppercase names are usually for macros
#define ASPECT_RATIO 1.653
const double AspectRatio = 1.653;int f(int parm) {std::cout << parm << std::endl;return parm;
}#define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))// We don't know what T is so pass by reference-to-const	Item20
template<typename T>
inline void callWithMax(const T& a, const T& b)
{f(a > b ? a : b);
}int main()
{std::cout << "测试常量指针" << std::endl;const char* const authorName1 = "StevePro Studios";   //c类型字符串 char*   //std::cout << *(authorName1 + 0) << *(authorName1 + 1) << std::endl;//char* const authorName12 = "StevePro Studios2";//authorName12[2] = 'x';  //指针只能指向S的地址(值可变)  authorName12[2] =nullptr;不可修改//const char* authorName13 = "StevePro Studios3";//authorName13 = nullptr; //指针可以指向其他内存空间,但内存空间中的值不能变。  authorName13[2] = 'x';不可修改std::cout << "string常量" << std::endl;const std::string authorName2("StevePro Studios");    //c类型字符串 stringstd::cout << "测类中的静态常量" << std::endl;std::cout << GamePlayer1::NumTurns << std::endl;GamePlayer2 g2;std::cout << g2.scores << std::endl;std::cout << "测类中的枚举" << std::endl;GamePlayer2 g3;std::cout << g3.NumTurns << std::endl;std::cout << "测试#define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))" << std::endl;int a = 5; int b = 2;CALL_WITH_MAX(++a, b);  std::cout << "a的值最后为: " << a << std::endl;a = 5; b = 2;CALL_WITH_MAX(++a, b + 10);   std::cout << "a的值最后为: " << a << std::endl;std::cout << "使用模板后" << std::endl;a = 5; b = 2;callWithMax(++a, b);a = 5; b = 2;callWithMax(a, b+10);return 0;
}

在这里插入图片描述
宏函数存在的问题
在这里插入图片描述

解决办法:
在这里插入图片描述

http://www.hkea.cn/news/602967/

相关文章:

  • 让顾客心动的句子seo模拟点击软件源码
  • 设计类专业包括哪些kj6699的seo综合查询
  • 手机网站制作哪家好查关键词
  • 米拓企业网站管理系统电商培训机构排名前十
  • 做效果图有哪些网站seo点击排名
  • 网络营销推广网站收录seo推广排名平台有哪些
  • 产品经理如何看待网站开发广州软件系统开发seo推广
  • wordpress 忘记管理员如何做网站seo
  • app和网站哪个有优势淘宝关键词排名
  • wordpress该域名宁波网站seo公司
  • 建购物网站怎么建呀简单的网站建设
  • 江苏省建设教育协会网站首页百度知道合伙人答题兼职入口
  • 做优化的网站平台搭建
  • 做网站需要多久网络推广是什么专业
  • 厦门加盟网站建设线上推广营销
  • 定制网站案例seo搜索引擎优化薪酬
  • 网站制作成功后怎么使用浏览器观看b站视频的最佳设置
  • 一家专门做开网店的网站北京seo专员
  • 专业企业网站搭建服务头条权重查询
  • 去哪儿网站上做民宿需要材料免费的黄冈网站有哪些平台
  • 网站建设网现在推广什么app最挣钱
  • 嘉兴装修公司做网站安装百度到桌面
  • 电商网站特点外贸营销网站建站
  • 上海市住房城乡建设管理委员会网站网络营销软文范例大全800
  • 莱芜区政协网站做网络优化的公司排名
  • 太原网站建设开发公司电商运营基本知识
  • php做企业网站seo网站推广企业
  • 万网网站备案授权书免费发布推广信息的b2b
  • 乡镇可以做门户网站seo是什么意思职业
  • 建设银行网站优点做个公司网站大概多少钱