复制构造函数从 const 转换为非 const?

发布于 2024-12-02 14:48:49 字数 382 浏览 0 评论 0原文

考虑以下问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

肥爪爪 2024-12-09 14:48:50

不,它不会转换为非 const 对象。考虑一下:

A a(42);
const A another = a;

这里,another 是一个 const 对象,是从非 const 对象创建的。

然而,更重要的是构造函数从现有对象创建一个新对象。新对象是否为 const 并不取决于现有对象。 const/非const旧/新对象的所有四种可能组合都是可能的。

No, it's not converting to a non-const object. Consider this:

A a(42);
const A another = a;

Here, another is a const 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 of const/non-const old/new objects are possible.

听风吹 2024-12-09 14:48:50

A(int) 构造函数从 int 转换为 A 的意义上来说,是的,您的复制构造函数 A(const A&)const A“转换”为 A。就此而言,它也会从非 const A“转换”为 A,因为 const 引用参数可以绑定到其中任何一个。

由于创建 const 对象与创建非 const 对象使用相同的构造函数,因此该复制构造函数也可以从 Aconst A “转换”为 <代码>常量A。

我在引号中使用“转换”只是因为从类型转换为其自身或自身的 cv 限定版本可能是该术语的令人困惑的使用,通常您只是将其称为“复制”而不是转换。

构造函数参数也可以绑定到 A 的派生类的实例,因此您可能会说它将派生类转换为 A。这通常称为“切片”。

严格来说,复制构造函数本身并不转换任何内容,但到 A 的转换(无论是强制转换还是隐式转换)确实依赖于使用匹配的构造函数。所以我认为施工方可以承担很大一部分功劳。

In the sense that the A(int) constructor converts from int to A, yes it's true that your copy ctor A(const A&) "converts" from const A to A. For that matter it also "converts" from non-const A to A, 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 or const A to const 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 to A. 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.

〆凄凉。 2024-12-09 14:48:49

复制构造函数创建现有对象的新副本,该对象可能是也可能不是 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 change other 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.

给我一枪 2024-12-09 14:48:49

不知道你的意思。 A(const A&) 是一个典型的复制因子,它对其唯一参数具有“只读”访问权限。如果你传递任何常量,一切都很好。如果你传递任何非 const 的东西,对于 ctor 来说它就会变成 const 。正如您所说, A a = 1 是一个转换因子。 A other = a 是一个复制因子。问题是什么?

关于你的问题的标题,在 C++ 中,没有公平方法将const转换为非const

class A
{
public:
    int xx;
    A(const A& other)
    {
        cout << "A cctor" << endl;
        /*  do some stuff */
        // other is const here - you can only call its
        // const methods and read all its data members            
    }

    A(int x) : xx(x) {}  /* conversion constructor */
        // at this point, x is not const, but it's a copy
        // of an object you've passed here, not the object itself
        // you can change x, it just doesn't matter - you
        // change the copy
    };


int main()
{    
    A a = 1; // a is not const, 1 is passed "by value", since it's primitive type
    A other = a; // a is not const, other is not const, a is passed by const reference
    return 0;
}

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.

class A
{
public:
    int xx;
    A(const A& other)
    {
        cout << "A cctor" << endl;
        /*  do some stuff */
        // other is const here - you can only call its
        // const methods and read all its data members            
    }

    A(int x) : xx(x) {}  /* conversion constructor */
        // at this point, x is not const, but it's a copy
        // of an object you've passed here, not the object itself
        // you can change x, it just doesn't matter - you
        // change the copy
    };


int main()
{    
    A a = 1; // a is not const, 1 is passed "by value", since it's primitive type
    A other = a; // a is not const, other is not const, a is passed by const reference
    return 0;
}
暮倦 2024-12-09 14:48:49

构造函数初始化一个新副本。而且从常量复制也没有问题。

不涉及转换。

Constructor initializes a new copy. And there is no problem in copying from a constant.

No conversion is involved.

橘虞初梦 2024-12-09 14:48:49

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.

叫思念不要吵 2024-12-09 14:48:49

否,复制构造函数通过将另一个类对象作为参数来创建类对象的副本。

由于为了构造作为参数传递的新对象,不需要对其进行修改,因此它作为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文