关于在 C++ 中将类作为别名参数传递的问题?
我有以下代码:
class Student {
private:
int no;
char name[14];
public:
void display() const;
Student(const Student& student); // Line 1
};
我已经读到类是引用类型,那么为什么在上述代码的第 1 行中声明为别名。 第 1 行
是否相当于: Student(const Student Student);
?
Possible Duplicate:
Why should the copy constructor accept its parameter by reference in C++?
I have the following code:
class Student {
private:
int no;
char name[14];
public:
void display() const;
Student(const Student& student); // Line 1
};
I have read that class is a reference type, so why in Line 1 of the above code declared as alias.
Is Line 1
equivalent to: Student(const Student student);
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 C++ 中,“类是引用类型”的断言毫无意义。您可能听说过与 C# 相关的内容,但这是完全不同的事情。
因此,整个讨论毫无意义。要了解复制构造函数的语法,您首先需要了解引用(这些确实存在于 C++ 中)和一般类。为初学者准备一本优秀的 C++ 书籍。
In C++, the assertion that “class is a reference type” makes no sense. You may have heard this in connection with C# but this is a completely different matter.
Consequently, the whole discussion is moot. To understand the syntax of copy constructors you first need to understand references (those do exist in C++) and classes in general. Grab a good C++ book for beginners.
首先我会使用
and
Student(const Student Student);
与Student(const Student& Student);
不同First I would use
instead of
and
Student(const Student student);
is NOT the same asStudent(const Student& student);
类不是引用类型。您可能是从 C# 或其他东西得到的。您必须将
&
添加到类型名称以使其成为引用。因此,Student
是一个学生对象,而Student&
是对Student
对象的引用。Classes are not reference types. You probably got that from C# or something. You have to add
&
to a type name to make it a reference. SoStudent
is a student object, whereasStudent&
is a reference to aStudent
object.这根本不是相同的代码。
通过引用传递参数会将确切的对象推送到函数的参数堆栈上。按值传递一个将推送一份副本。
如果您在没有引用的情况下声明复制构造函数,则会出现无限循环,因为程序将尝试创建调用复制构造函数的副本,而复制构造函数又会尝试创建副本,依此类推。
It is not at all the same code.
Passing a parameter by reference will push the exact object on the function's argument stack. Passing one by value will push a copy.
If you declare your copy constructor without the reference, you'll get an infinite loop, as the program will try to create a copy calling the copy constructor, which in turn will try to create a copy and so on.
类不是引用类型。它是 C++ 中的用户定义类型。当我们使用
&
作为(成员)函数参数的一部分时,这意味着我们收到了传递的参数的引用。对引用所做的任何修改实际上都会修改传递的参数本身。而在另一种情况下,则会创建对象的副本。Class is not a reference type. It is a user defined type in C++. When ever we use
&
as a part of (member) function argument(s), it means we receive a reference of the parameter(s) passed. Any modification made to the reference will actually modify the passed parameter itself. While in the other case, a copy of the object is made.类是许多托管语言(如 Java 或 C#)中的引用类型。但在 C++ 中却并非如此。如果您编写
Student(const Student Student);
,则意味着您定义了一个构造函数,该构造函数采用一个 Student 类型的参数按值。本质上是将对象复制到函数的作用域中。同样在 C++ 中,我们调用这样的语法:
Student(const Student& Student);
通过引用传递,而不是别名(本质上是相同的,但在阅读某些相关主题时可能会感到困惑)。Class is a reference type in many managed languages (like Java or C#). It is not the case however in C++. If you write
Student(const Student student);
it means you define a constructor taking one parameter of type Student by value. In essence copying the object into the scope of the function.Also in C++ we call syntax like this:
Student(const Student& student);
passing by reference, not alias (in essence the same, but it may be confusing when reading up on some related subject).