【问题标题】:difference between Student& a, Student &a;Student&a、Student&a的区别;
【发布时间】:2016-04-19 14:46:45
【问题描述】:

我对这些术语感到困惑。

假设我们有一个Student类,那么

class Student{
   public:
     Student(const Student& a){ ... }
     Student(const Student &a){ ... }
};

我想问一下以下术语的含义

Student& a;
Student &a;

这些用于复制构造函数。 我知道它们主要用于深浅复制构造函数。

【问题讨论】:

  • 完全没有区别。只是作者的味道。
  • 除了可读性没有区别。
  • @UmarAsghar 名为a 的参数是referenceconst Student。也就是说,它引用了在其他地方声明的Student 对象,并且不能修改它。另一个tutorial
  • @KerrekSB 不,应该是Student&Student&&
  • @BoBTFish:等待构建...

标签: c++ oop constructor copy-constructor default-constructor


【解决方案1】:
int   i   =   5;
^^^   ~       =
Type  Name  Value
(int) (i)    (5)


int&   ref   =  i;
^^^^^  ~~~      =
Type   Name   Value
(int&) (ref)   (i)


int  & ref   =  i;
^^^^^^ ~~~      =
Type   Name   Value
(int&) (ref)   (i)


int   &ref   =  i;
^^^^^^^~~~      =
 Type  Name   Value
(int&) (ref)   (i)

所以基本上这两个:

Student& a;
Student &a;

或多或少是一样的东西。

至于拷贝构造函数的目的是什么,看看:

【讨论】:

  • 您真的是要使用nullptr 初始化对int 的引用吗?我很惊讶编译。
【解决方案2】:

当我开始学习 C 时,我对指针语法感到困惑。 char* c;char * c;char *c; 对我来说似乎不同,我对 C 中的这么多类型感到害怕。

但事实证明,您可以在 'main' 类型名称(例如 intchar,甚至是指针等)和变量名称之间的任意位置放置星号.这意味着,所有这些声明都是相同的。

这也适用于 C++ 中的引用。实际上,这取决于开发人员的编码风格,而含义不会改变。

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2021-12-02
    • 2011-08-27
    • 2016-02-17
    • 2022-01-12
    • 2019-06-02
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多