复制构造函数从 const 转换为非 const?
考虑以下问题:
class A
{
public:
int xx;
A(const A& other)
{
cout << "A cctor" << endl;
/* do some stuff */
}
A(int x) : xx(x) {} /* conversion constructor */
};
int main()
{
A a = 1;
A other = a;
return 0;
}
在这种情况下(以及一般情况下)说 CCtor 从 const 转换为非 const 是否正确?
谢谢,罗恩
Consider the following :
class A
{
public:
int xx;
A(const A& other)
{
cout << "A cctor" << endl;
/* do some stuff */
}
A(int x) : xx(x) {} /* conversion constructor */
};
int main()
{
A a = 1;
A other = a;
return 0;
}
Is it right to say that CCtor converts from const to non-const in this case (and also in general) ?
Thanks ,Ron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,它不会转换为非 const 对象。考虑一下:
这里,
another
是一个const
对象,是从非const
对象创建的。然而,更重要的是构造函数从现有对象创建一个新对象。新对象是否为 const 并不取决于现有对象。
const
/非const
旧/新对象的所有四种可能组合都是可能的。No, it's not converting to a non-
const
object. Consider this:Here,
another
is aconst
object, created from a non-const
object.More important, however, is that a constructor creates a new object from an existing one. Whether that new object is
const
or not does not depend on the existing object. All four possible combinations ofconst
/non-const
old/new objects are possible.从
A(int)
构造函数从int
转换为A
的意义上来说,是的,您的复制构造函数A(const A&)
从const A
“转换”为A
。就此而言,它也会从非 constA
“转换”为A
,因为 const 引用参数可以绑定到其中任何一个。由于创建 const 对象与创建非 const 对象使用相同的构造函数,因此该复制构造函数也可以从
A
或const A
“转换”为 <代码>常量A。我在引号中使用“转换”只是因为从类型转换为其自身或自身的 cv 限定版本可能是该术语的令人困惑的使用,通常您只是将其称为“复制”而不是转换。
构造函数参数也可以绑定到
A
的派生类的实例,因此您可能会说它将派生类转换为A
。这通常称为“切片”。严格来说,复制构造函数本身并不转换任何内容,但到
A
的转换(无论是强制转换还是隐式转换)确实依赖于使用匹配的构造函数。所以我认为施工方可以承担很大一部分功劳。In the sense that the
A(int)
constructor converts fromint
toA
, yes it's true that your copy ctorA(const A&)
"converts" fromconst A
toA
. For that matter it also "converts" from non-constA
toA
, since the const reference parameter can bind to either.And since the same constructor is used to create a const object as to create a non-const one, that copy ctor can also "convert" from
A
orconst A
toconst A
.I've used "convert" in quotes just because converting from a type to itself or a cv-qualified version of itself is perhaps a confusing use of the term, normally you just call that "copying" rather than conversion.
The constructor parameter can bind to an instance of a derived class of
A
too, so you might say that it converts derived classes toA
. That's normally called "slicing".Strictly speaking it's not the copy ctor itself that converts anything, but a conversion to
A
(whether a cast or an implicit conversion) does depend on using a matching constructor. So I suppose the constructor can claim a large part of the credit.复制构造函数创建现有对象的新副本,该对象可能是也可能不是 const。
A::A(const A& other)
中的 const 只是表示我们不会更改复制构造函数中的other
。事实上,如果您尝试修改 ctor 内部的其他内容,编译器会向您抱怨。创建的对象也可能是或不是 const,具体取决于您如何声明它。
A copy constructor creates a new copy of an existing object, that object may or may not be const. The const in
A::A(const A& other)
just says we are not going to changeother
in the copy ctor. Indeed if you attempt to modify other inside the ctor the compiler will moan at you.The created object also may or may not be const depending on how you declare it.
不知道你的意思。
A(const A&)
是一个典型的复制因子,它对其唯一参数具有“只读”访问权限。如果你传递任何常量,一切都很好。如果你传递任何非 const 的东西,对于 ctor 来说它就会变成 const 。正如您所说,A a = 1
是一个转换因子。A other = a
是一个复制因子。问题是什么?关于你的问题的标题,在 C++ 中,没有公平方法将
const
转换为非const
。No idea what you mean.
A(const A&)
is a typical copy-ctor, which has a "read-only" access to its only argument. If you pass anything const, everything is fine. If you pass anything non-const, for ctor it becomes const.A a = 1
is a conversion ctor, as you said.A other = a
is a copy ctor. What's the question?Regarding your question's title, in C++ there's no fair way to convert
const
to non-const
.构造函数初始化一个新副本。而且从常量复制也没有问题。
不涉及转换。
Constructor initializes a new copy. And there is no problem in copying from a constant.
No conversion is involved.
CCtor 从 const 转换为非 const 是什么意思?
如果您的意思是,通过调用复制构造函数从 const 对象创建非常量对象,那么是的。但这并不意味着 const 对象本身在复制构造函数内(或在调用站点)变成非 const。它仅意味着新构造的对象是通过复制现有对象来创建的,该对象作为常量引用传递给复制构造函数。
What do you mean by CCtor converts from const to non-const?
If you mean, the non-const object gets created from the const object by invoking the copy-constructor, then yes. But that doesn't mean the const-object itself becomes non-const inside the copy-constructor (or at the call site). It only means that the newly constructed object is created by copying the existing object which is passed as const reference to the copy-constructor.
否,复制构造函数通过将另一个类对象作为参数来创建类对象的副本。
由于为了构造作为参数传递的新对象,不需要对其进行修改,因此它作为 const 传递。
No the copy constructor creates an copy of the class object by taking another class object as the parameter.
Since in order to construct the new object being passed as parameter is not required to be modified it it passed as an
const
.