即使从未调用过,是否也需要副本 CTOR?

发布于 2024-11-15 06:44:44 字数 769 浏览 3 评论 0原文

考虑以下内容:

class X {
public:
    X(int i) { cout << "X(int i)" << endl; }
    X(const X& x) { cout << "X(const X& x)" << endl; }
};

void main() {
    X x1(1);
    X x2 = X(1);
    X x3 = (X)1;
}

运行此代码会产生以下输出:

X(int i)
X(int i)
X(int i)

我认为上述所有三个语句都是等效的,因为复制 CTOR 从未被调用。但是,将 X 的副本 CTOR 更改为私有:

class X {
public:
    X(int i) { cout << "X(int i)" << endl; }
private:
    X(const X& x) { cout << "X(const X& x)" << endl; }
};

将无法编译(在 Visual Studio 2010 中)并出现此错误:

cannot access private member declared in class 'X'

所以看来副本 CTOR 以某种方式涉及,尽管我不太明白如何。

谢谢

consider the following:

class X {
public:
    X(int i) { cout << "X(int i)" << endl; }
    X(const X& x) { cout << "X(const X& x)" << endl; }
};

void main() {
    X x1(1);
    X x2 = X(1);
    X x3 = (X)1;
}

running this code produces this output:

X(int i)
X(int i)
X(int i)

I thought that all of the above three statements are equivalent as the copy CTOR is never called. However, changing X's copy CTOR to be private:

class X {
public:
    X(int i) { cout << "X(int i)" << endl; }
private:
    X(const X& x) { cout << "X(const X& x)" << endl; }
};

Will fail to compile (In visual studio 2010) with this error:

cannot access private member declared in class 'X'

So it seems the copy CTOR is involved somehow though I don't quite understand how.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

誰ツ都不明白 2024-11-22 06:44:44
X x1(1);
X x2 = X(1);
X x3 = (X)1;

原因是所有这些并不完全等同。

第一个是直接初始化,第二个和第三个是复制初始化。对于复制初始化,复制构造函数必须是公共的,否则编译器将给出错误。

现在的问题是,如果第二个和第三个要求复制向量是公共的,那么为什么会出现以下输出:

X(int i)
X(int i)
X(int i)

这肯定表明复制向量永远不会被调用,这是正确的。编译器只是省略了对 copy-ctor 的调用。根据§8.5/14,在这种情况下,编译器可以消除调用复制构造函数的需要。这就是为什么你看不到 copy-ctor 被调用的原因。

内部一点:在第二种和第三种情况下,首先通过调用 X(int i) 创建一个临时文件,然后这个临时文件应该被传递给复制向量来复制初始化对象被宣布。但编译器优化了这一步,省略了对 copy-ctor 的调用。

X x1(1);
X x2 = X(1);
X x3 = (X)1;

The reason is that all of these are not exactly equivalent.

First one is direct-initialization, while the second and third is copy-initialization. For copy-initialization, copy-constructor must be public, or else compiler will give error.

Now the question is, if 2nd and 3rd requires the copy-ctor to be public,then why the following output:

X(int i)
X(int i)
X(int i)

This surely says that copy-ctor is never called which is true. Compiler just elided the call to copy-ctor. According to §8.5/14, in such cases, the compiler is permitted to eliminate the need to call copy-constructor. That is why you don't see copy-ctor being called.

A little inside : in the 2nd and 3rd case, first a temporary is created by calling X(int i), then this temporary was supposed to be passed to the copy-ctor to copy-initialize the object being declared. But the compiler optimizes away this step, eliding the call to copy-ctor.

二智少女 2024-11-22 06:44:44

X x2 = ... 调用复制构造函数(即使编译器稍后将其优化)。因此,它必须仍然是可访问的。

The X x2 = ... invokes the copy constructor (even if the compiler optimises it out later). Thus, it must still be accessible.

平生欢 2024-11-22 06:44:44
  1. 这个:

    X x3 = (X)1;
    是从 intX

    类型的对象的 C 风格转换

  2. 此复制:

    X x2 = X(1);

已优化,但编译器仍然需要访问复制构造函数。

  1. This :

    X x3 = (X)1;
    is c-style cast from int into the object of type X

  2. This copying :

    X x2 = X(1);

is optimized away but the compiler still needs the access to the copy-constructor.

是伱的 2024-11-22 06:44:44

如您所知,Ist ​​对象使用参数化构造函数,而所有其他对象都使用复制构造函数。即为什么当您将其设置为私有访问冲突时会发生。另外两个对象正在使用复制构造函数。

这些是使用复制构造函数的方式。

Ist object makes use of parrametrized constructor as you know it and all others are using copy constructor. i.e. why when you are makin it private access violation occurs. The others two objects are making use of copy constructor.

These are the way of using copy constructor.

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