与类的成员复制混淆
研究了C++中的成员复制和赋值运算符后, 并查看“ 类的成员复制 ”,它解释了默认分配的条件无法生成运算符。我不太清楚这些概念,因为我尝试的以下示例实际上适用于 g++4.5
#include<iostream>
using namespace std;
class Y{
int& x;
const int cx;
public:
Y(int v1,int v2)
:x(v1),cx(v2)
{}
int getx(){return x;}
int getcx(){return cx;}
};
int main()
{
int a = 10;
Y y1(a,a);
Y y2 = y1;//assignment
cout<<y1.getx()<<" "<<y1.getcx();
return 0;
}
那么我在哪里没有得到这些概念。请建议其他示例(如果可能),以便我更好地理解。
After studying the member copying and the assignment operator in C++,
and looking at " member copying of class " which explains the conditions where default assignment operator cannot be generated. I am not very clear about the concepts as the following example I tried actually works on g++4.5
#include<iostream>
using namespace std;
class Y{
int& x;
const int cx;
public:
Y(int v1,int v2)
:x(v1),cx(v2)
{}
int getx(){return x;}
int getcx(){return cx;}
};
int main()
{
int a = 10;
Y y1(a,a);
Y y2 = y1;//assignment
cout<<y1.getx()<<" "<<y1.getcx();
return 0;
}
So where am I not getting the concepts. Please suggest other examples (if possible) so that I can understand better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Y y2 = y1;
不是赋值。这是一个复制构造函数调用。如果在同一行声明并初始化变量,则会调用单参数构造函数,并将等号右侧作为参数。Y
并不能阻止默认复制构造函数被实例化(和调用)。尝试以下操作:
这应该会失败,尽管我现在无法测试它。
Y y2 = y1;
is not an assignment. It's a copy constructor call. If you declare and initialize a variable on the same line, a one parameter constructor is called with the right hand side of the equals sign as the parameter. There's nothing aboutY
that prevents the default copy constructor from being instantiated (and called).Try the following:
This should fail, although I can't test it right now.
x
是 int 的引用变量。现在您将其初始化为v1
,这意味着x
是v1
本身的别名。v1
的范围仅在构造函数中。话虽如此 -相当于
x
is a reference variable to an int. Now you are initializing it tov1
, which meansx
is an alias tov1
itself. The scope ofv1
is in the constructor alone. With that said -is equivalent to
您的类包含不能默认构造或赋值的成员,即:
引用
常量
因此,您的类不能隐含默认构造函数或赋值运算符。例如,您必须编写自己的构造函数:
很明显,您不能默认构造
Foo
(即)。同样清楚的是,您不能重新分配Foo x;
Foo
类的对象(即),因为您无法重新分配引用或常量。x = y;
通过为您的类提供引用或常量成员,您实际上可以在类本身上赋予引用或常量语义(如果您愿意),因此这应该是一个相当直接的逻辑结果。例如,重新分配可能在语义上甚至没有意义,因为您的类应该体现一个恒定的概念。
但是,请注意,您可以制作您的类的副本:这是因为您可以制作引用(即更多别名)的“副本”以及常量的副本。因此,通过简单地应用逐个成员的复制构造函数,复制构造函数就可以隐式地使用。所以你可以说:
这会产生另外两个对象
y
和z
,它们具有ya == 15
和 < code>za == 15、yb
和zb
都是对n
的引用。 (不要对最后的两种替代语法感到困惑;两者调用复制构造函数。)Your class contains members which cannot be default-constructed or assigned, namely:
References
Constants
Therefore, no default constructor or assignment operator can be implied for your class. For example, you have to write your own constructor:
It is clear that you cannot default-construct
Foo
(i.e.). It is also clear that you cannot reassign objects of classFoo x;
Foo
(i.e.), because you cannot reassign references or constants.x = y;
By giving your class a reference or a constant member, you actually confer reference or constant semantics on the class itself, if you will, so this should be a fairly immediate logical consequence. For example, reassignment probably doesn't even make sense semantically, because your class is supposed to embody a constant concept.
However, note that you can make copies of your class: That's because you can make "copies" of references (i.e. furhter aliases), and copies of constants. Therefore, a copy constructor is available implicitly, by simply applying copy-construction member-by member. So you can say:
This results in two further objects
y
andz
which havey.a == 15
andz.a == 15
, andy.b
andz.b
are all references ton
. (Don't be confused by the two alternative syntaxes at the end; both invoke the copy constructor.)