gpt4 book ai didi

c++ - 为什么 `CClientDC dc (this);`不能替换为 `CClientDC dc = CClientDC(this);`

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:17 36 4
gpt4 key购买 nike

我是 MFC 编程的新手。这些天我正在使用 Jeff Prosise 的书来学习 MFC 编程。我遇到过:CClientDC dc (this); 我从 CClientDC 类的定义中找到了一个构造函数:CClientDC(CWnd* pWnd); 但如果我替换 CClientDC dc (this);CClientDC dc = CClientDC(this);。编译器告诉我这是一个错误。我无法理解。我认为 CClientDC dc (this);CClientDC dc = CClientDC(this); 是一回事。或者换句话说,前者是一种简单的形式。

非常感谢。

最佳答案

CClientDC dc = CClientDC(this);

相当于

CClientDC dc(CClientDC(this));

为此,CClientDC 必须有一个复制或移动构造函数,因为您基本上是通过从临时对象复制来构造 dc

If the initialization is [...] copy-initialization where the cv-unqualified version of the source type is the same class as [...] the destination, constructors are considered. [...] The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.

关于c++ - 为什么 `CClientDC dc (this);`不能替换为 `CClientDC dc = CClientDC(this);`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875874/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com