C++ 复制构造函数语法:& 符号是否引用 r/l 值?
C++ copy constructor syntax: Is ampersand reference to r/l values?
以下是我的 C++ 文本的摘录,说明了使用复制构造函数声明 class 的语法。
class Student {
int no;
char* grade;
public:
Student();
Student(int, const char*);
Student(const Student&);
~Student();
void display() const;
};
复制构造函数,如下所示:
Student(const Student&);
在 参数 Student 之后有一个符号 。
在 C 和 C++ 中,我相信,符号字符用作指针的 'address of' 运算符。当然,在指针名称之前使用 & 字符 是标准的,而复制构造函数在指针名称之后使用它,所以我认为这不是同一个运算符。
我发现的符号字符的另一种用法与右值和左值有关,如下所示:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
我的问题不是关于右值和左值,我只是想知道为什么 & 字符放在参数之后,以及什么这被称为 if/why 是必要的。
C++ 具有 C 中不存在的引用类型。&
用于定义此类类型。
int i = 10;
int& iref = i;
这里iref
是对i
的引用。
对 i
所做的任何更改都可以通过 iref
看到,对 iref
所做的任何更改都可以通过 i
看到。
iref = 10; // Same as i = 10;
i = 20; // Same as iref = 20;
引用可以是左值引用或右值引用。在上面的例子中,iref
是一个左值引用。
int&& rref = 10;
这里rref
是一个右值引用。
您可以在 http://en.cppreference.com/w/cpp/language/reference 阅读更多关于右值引用的信息。
以下是我的 C++ 文本的摘录,说明了使用复制构造函数声明 class 的语法。
class Student {
int no;
char* grade;
public:
Student();
Student(int, const char*);
Student(const Student&);
~Student();
void display() const;
};
复制构造函数,如下所示:
Student(const Student&);
在 参数 Student 之后有一个符号 。
在 C 和 C++ 中,我相信,符号字符用作指针的 'address of' 运算符。当然,在指针名称之前使用 & 字符 是标准的,而复制构造函数在指针名称之后使用它,所以我认为这不是同一个运算符。
我发现的符号字符的另一种用法与右值和左值有关,如下所示:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
我的问题不是关于右值和左值,我只是想知道为什么 & 字符放在参数之后,以及什么这被称为 if/why 是必要的。
C++ 具有 C 中不存在的引用类型。&
用于定义此类类型。
int i = 10;
int& iref = i;
这里iref
是对i
的引用。
对 i
所做的任何更改都可以通过 iref
看到,对 iref
所做的任何更改都可以通过 i
看到。
iref = 10; // Same as i = 10;
i = 20; // Same as iref = 20;
引用可以是左值引用或右值引用。在上面的例子中,iref
是一个左值引用。
int&& rref = 10;
这里rref
是一个右值引用。
您可以在 http://en.cppreference.com/w/cpp/language/reference 阅读更多关于右值引用的信息。