现在的位置: 首页 > 综合 > 正文

[unresolved]《The C++ Standard Library》第37页 pair的几种初始化

2011年12月12日 ⁄ 综合 ⁄ 共 1029字 ⁄ 字号 评论关闭

第37页最后几行:

没有看明白。

下面一句一句的分析:

1.

1.1 看看f(p)的定义:

执行函数f,传递参数的过程中,就是复制p(42,”hello”),然后在函数体内操作。

相当于pair<int,const char*> p2 = p;

再细化一下,就是调用以下的构造函数:

可是上面却说调用build in default copy constructor.难道是这样的过程?

Pair<int, const char*> p = p();

p.first = 42;

p.second = “hello”;

【注】我想可能的原因是42是int并非const int.所以与g中的参数并不匹配,这时就调用了默认的构造函数,然后执行:

Pair<int, const char*> p = p();

p.first = 42;

p.second = “hello”;

[unresolved]《The C++ Standard Library》第37页 pair的几种初始化

第37页最后几行:

没有看明白。

下面一句一句的分析:

1.

1.1 看看f(p)的定义:

执行函数f,传递参数的过程中,就是复制p(42,”hello”),然后在函数体内操作。

相当于pair<int,const char*> p2 = p;

再细化一下,就是调用以下的构造函数:

可是上面却说调用build in default copy constructor.难道是这样的过程?

Pair<int, const char*> p = p();

p.first = 42;

p.second = “hello”;

【注】我想可能的原因是42是int并非const int.所以与g中的参数并不匹配,这时就调用了默认的构造函数,然后执行:

Pair<int, const char*> p = p();

p.first = 42;

p.second = “hello”;

也不知道我的猜测是否正确

2.

2.1 看看g (p) 的定义

执行函数g,传递参数的过程中,就是复制p(42,”hello”),然后在函数体内操作。

相当于pair<const int,std::string> p2 = p;

由于42恰好是const int, “hello”恰好是std::string.

所以直接调用构造函数:

2.

2.1 看看g (p) 的定义

执行函数g,传递参数的过程中,就是复制p(42,”hello”),然后在函数体内操作。

相当于pair<const int,std::string> p2 = p;

由于42恰好是const int, “hello”恰好是std::string.

所以直接调用构造函数:

抱歉!评论已关闭.