关于在 C++ 中将类作为别名参数传递的问题?

发布于 2024-12-03 12:31:41 字数 529 浏览 0 评论 0原文

可能的重复:
为什么复制构造函数应该接受其参数在 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 技术交流群。

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

发布评论

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

评论(6

烟雨凡馨 2024-12-10 12:31:41

我读到类是引用类型

在 C++ 中,“类是引用类型”的断言毫无意义。您可能听说过与 C# 相关的内容,但这是完全不同的事情。

因此,整个讨论毫无意义。要了解复制构造函数的语法,您首先需要了解引用(这些确实存在于 C++ 中)和一般类。为初学者准备一本优秀的 C++ 书籍

I have read that class is a reference type

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.

花开柳相依 2024-12-10 12:31:41

首先我会使用

std::string name

and Student(const Student Student);

char name[14]

Student(const Student& Student); 不同

Student(const Student student) //is a copy
Student(const Student& student) // is a reference to the object

First I would use

std::string name

instead of

char name[14]

and Student(const Student student); is NOT the same as Student(const Student& student);

Student(const Student student) //is a copy
Student(const Student& student) // is a reference to the object
旧竹 2024-12-10 12:31:41

类不是引用类型。您可能是从 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. So Student is a student object, whereas Student& is a reference to a Student object.

野生奥特曼 2024-12-10 12:31:41

这根本不是相同的代码。

通过引用传递参数会将确切的对象推送到函数的参数堆栈上。按值传递一个将推送一份副本。

如果您在没有引用的情况下声明复制构造函数,则会出现无限循环,因为程序将尝试创建调用复制构造函数的副本,而复制构造函数又会尝试创建副本,依此类推。

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.

嘿哥们儿 2024-12-10 12:31:41

类不是引用类型。它是 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.

千寻… 2024-12-10 12:31:41

类是许多托管语言(如 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).

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