第37页最后几行:

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

没有看明白。

下面一句一句的分析:

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

1.1 看看f(p)的定义:

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

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

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

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

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

可是上面却说调用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页最后几行:

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

没有看明白。

下面一句一句的分析:

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

1.1 看看f(p)的定义:

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

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

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

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

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

可是上面却说调用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. [unresolved]《The C++ Standard Library》第37页 pair的几种初始化

2.1 看看g (p) 的定义

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

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

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

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

所以直接调用构造函数:

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

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

2.1 看看g (p) 的定义

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

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

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

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

所以直接调用构造函数:

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

相关文章:

  • 2021-08-28
  • 2022-01-14
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
猜你喜欢
  • 2021-11-26
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2021-08-20
  • 2021-11-20
相关资源
相似解决方案