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

网站及数据库怎么做后门google搜索入口

网站及数据库怎么做后门,google搜索入口,西安网站建设哪家专业,商务贸易网站建设一直对C初始化使用圆括号和花括号的区别有所疑惑,参考书籍和博客简单总结一下 文章目录 常见的初始化操作统一初始化(Uniform Initialization)初始化列表(Initializer Lists) 常见的初始化操作 对于一个基础数据类型进行初始化,比如 int: i…

一直对C++初始化使用圆括号和花括号的区别有所疑惑,参考书籍和博客简单总结一下

文章目录

  • 常见的初始化操作
  • 统一初始化(Uniform Initialization)
  • 初始化列表(Initializer Lists)


常见的初始化操作

对于一个基础数据类型进行初始化,比如 int

int x(0);
int y = 0;
int z{0};

对于用户定义的类型:

Foo foo1; // default construction
Foo foo2 = foo1; // equivalent to Foo foo2(foo1)
foo1 = foo2; // assignment operation, call operator= function

统一初始化(Uniform Initialization)

C++11之前,初始化一个变量或一个对象,可以使用圆括号()、花括号{} 以及赋值运算符。这就会对初学者产生一定疑惑(具体应该使用哪一种??)。对此,C++11 引入了统一初始化的概念,即对于任意初始化,可以使用一个统一的语法,一个花括号 {},举个栗子:

int values[] {1, 2, 3}; // initialize int array
std::vector<int> v{2, 3, 5, 7, 11, 13, 17}; // initialize vector<int>
std::vector<std::string> cities{"Berlin", "New York", "London", "Braunschweig", "Cairo", "Cologne"}; // implicit conversion: const char * => std::string
std::complex<double> c{4.0, 3.0}; // equivalent to c(4.0, 3.0)

一个初始化列表强迫进行值初始化,基础数据类型初始化为0,指针类型初始化为 nullptr

int i; // i has undefined value
int j{}; // j is initialized by 0
int* p; // p has undefined value
int* q{}; // q is initialized by nullptr

需要注意的是,如果存在收缩转换(narrowing),即精度减小,那么不能使用统一初始化:

int x1(5.3); // OK, but OUCH: x1 becomes 5
int x2 = 5.3; // OK, but OUCH: x2 becomes 5
int x3{5.0}; // ERROR: narrowing
int x4 = {5.3}; // ERROR: narrowing
char c1{7}; // OK: even though 7 is an int, this is not narrowing
char c2{99999}; // ERROR: narrowing (if 99999 doesn’t fit into a char)
std::vector<int> v1 { 1, 2, 4, 5 }; // OK
std::vector<int> v2 { 1, 2.3, 4, 5.6 }; // ERROR: narrowing doubles to ints

初始化列表(Initializer Lists)

为了支持用户定义类型的初始化列表,C++11 提供了模板类 std::initializer_list<>。其可被用于支持使用一列值进行初始化或对一列值进行处理。例如:

void print (std::initializer_list<int> vals)
{
for (auto p=vals.begin(); p!=vals.end(); ++p) { // process a list of values
std::cout << *p << "\n";
}
}
print ({12,3,5,7,11,13,17}); // pass a list of values to print()

当使用统一初始化时,如果构造函数同时匹配指定数量的构造函数以及使用初始化列表的构造函数,优先选择使用初始化列表的构造函数:

class P
{
public:
P(int,int);
P(std::initializer_list<int>);
};
P p(77,5); // calls P::P(int,int)
P q{77,5}; // calls P::P(initializer_list)
P r{77,5,42}; // calls P::P(initializer_list)
P s = {77,5}; // calls P::P(initializer_list)

如果构造函数为显示构造函数,那么不能使用 = 语法进行初始化,因为不能进行隐式转换:

class P
{
public:
P(int a, int b) {
...
}
explicit P(int a, int b, int c) {
...
}
};
P x(77,5); // OK
P y{77,5}; // OK
P z {77,5,42}; // OK
P v = {77,5}; // OK (implicit type conversion allowed)
P w = {77,5,42}; // ERROR due to explicit (no implicit type conversion allowed)void fp(const P&);
fp({47,11}); // OK, implicit conversion of {47,11} into P
fp({47,11,3}); // ERROR due to explicit
fp(P{47,11}); // OK, explicit conversion of {47,11} into P
fp(P{47,11,3}); // OK, explicit conversion of {47,11,3} into P

类似地,使用一个初始化列表的显示构造函数,禁用隐式转换,对于0个、1个或多个初值。

相比于使用圆括号的一个优势,比如当我们使用带参构造函数可以使用如下语法:

Foo foo1(10); // call Foo constrution with arguments

但使用类似语法调用无参构造函数时,却是声明了个函数,而不是创建对象:

Foo foo2(); // declare a foo2 function without arguments and return Foo object
Foo foo2; // foo2 is a default initialized object

我们可以使用花括号来解决这个歧义,因为花括号无法声明为函数

Foo f2{}; // no ambiguity
http://www.tj-hxxt.cn/news/38358.html

相关文章:

  • 网站设计开发文档模板高粱seo博客
  • 用dw做教学网站国外广告联盟平台
  • 网站第三方评价如何做百度推广排名怎么做的
  • 制作企业网站首页站长工具无内鬼放心开车禁止收费
  • 外贸网站建设网页成都网站建设技术外包
  • 写作网站新手2020新闻大事件摘抄
  • 重庆sem网站推广seo在线培训机构排名
  • 西安关键词排名提升宁波优化推广选哪家
  • 国外的网页制作网站网站推广怎么优化
  • 小企业网站建设平台免费二级域名平台
  • 网站相关推荐怎么做发软文
  • 网站做流量推广文章的推广渠道
  • 万能小偷程序做网站西安seo优化系统
  • 网站做推广页需要什么软件有哪些网站推广与优化平台
  • 新闻网站想备案新东方英语培训机构官网
  • 做信公众号首图的网站网站统计数据分析
  • 企业网站建设联系方式seo关键词怎么选
  • 高明做网站游戏推广员平台
  • 网站建设和技术支持重庆关键词seo排名
  • wap asp网站模板下载百度网络营销的概念
  • 互联网公司网站模板seo的五个步骤
  • 网站优化人员2023新闻热点摘抄
  • 网站建设成本计划深度优化
  • 做网站开发需要学什么软件自己建网页
  • 网站建设开发的目的做推广网络
  • 三亚房产做公示是什么网站友情链接交换统计表
  • 深圳外包网站制作公司滨州网站建设
  • 建设网站是哪个部门负责营销手机系统安装
  • 如何做网站广告图片青岛网站制作公司
  • ysl免费网站建设网络运营培训班多少钱