为什么Prefix Incrementor Overload返回Type&而不是类型?

时间:2016-02-17 17:36:05

标签: c++ operator-overloading prefix postfix-operator

刚开始做运算符重载并且我的老师没有深入到它们中所以我想知道为什么返回类型对于前缀/后缀增量/减量是不同的。当我看到前缀重载时,返回类型被写为Type&,但后缀的返回类型被写为Type。我没有和&并且功能都正常运行。返回类型是否会影响任何内容,或者它只是区分前缀和后缀的另一种方式吗?

3 个答案:

答案 0 :(得分:2)

原因是允许链接:

object(App\Models\Customer)#79 (23) {
  ["connection":protected]=>
  string(3) "web"
  ["table":protected]=>
  string(9) "customers"
  ["fillable":protected]=>
  array(15) {
    [0]=>
    string(5) "email"
    [1]=>
    string(8) "password"
    [2]=>
    string(10) "first_name"
    [3]=>
    string(9) "last_name"
    [4]=>
    string(12) "mobile_phone"
    [5]=>
    string(13) "address_line1"
    [6]=>
    string(13) "address_line2"
    [7]=>
    string(13) "address_line3"
    [8]=>
    string(5) "token"
    [9]=>
    string(5) "vcode"
    [10]=>
    string(9) "promocode"
    [11]=>
    string(14) "referral_count"
    [12]=>
    string(8) "verified"
    [13]=>
    string(11) "location_id"
    [14]=>
    string(7) "city_id"
  }
  ["primaryKey":protected]=>
  string(2) "id"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["timestamps"]=>
  bool(true)
  ["attributes":protected]=>
  array(19) {
    ["id"]=>
    int(3)
    ["first_name"]=>
    string(8) "Abubakar"
    ["last_name"]=>
    string(8) "Mohammed"
    ["email"]=>
    string(21) "xxx@gmail.com"
    ["password"]=>
    string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6Ov1NCBmfSypVQ4RoL20qL4M5YqvAz/vS"
    ["mobile_phone"]=>
    string(11) "07427356289"
    ["address_line1"]=>
    string(17) "25 Hamilton House"
    ["address_line2"]=>
    string(8) "Lonsdale"
    ["address_line3"]=>
    string(9) "Wolverton"
    ["city"]=>
    NULL
    ["vcode"]=>
    string(0) ""
    ["promocode"]=>
    string(8) "XXXXXXXX"
    ["referral_count"]=>
    int(0)
    ["token"]=>
    string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6OqJ.Hzr67xYdJj34w4XXkW2e_ioVv1Si"
    ["verified"]=>
    string(1) "1"
    ["created_at"]=>
    string(19) "2016-01-29 00:05:52"
    ["updated_at"]=>
    string(19) "2016-02-11 17:42:15"
    ["city_id"]=>
    int(1)
    ["location_id"]=>
    int(2)
  }
  ["original":protected]=>
  array(19) {
    ["id"]=>
    int(3)
    ["first_name"]=>
    string(8) "Abubakar"
    ["last_name"]=>
    string(8) "Mohammed"
    ["email"]=>
    string(21) "xxx@gmail.com"
    ["password"]=>
    string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6Ov1NCBmfSypVQ4RoL20qL4M5YqvAz/vS"
    ["mobile_phone"]=>
    string(11) "07427356289"
    ["address_line1"]=>
    string(17) "25 Hamilton House"
    ["address_line2"]=>
    string(8) "Lonsdale"
    ["address_line3"]=>
    string(9) "Wolverton"
    ["city"]=>
    NULL
    ["vcode"]=>
    string(0) ""
    ["promocode"]=>
    string(8) "XXXXXXXX"
    ["referral_count"]=>
    int(0)
    ["token"]=>
    string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6OqJ.Hzr67xYdJj34w4XXkW2e_ioVv1Si"
    ["verified"]=>
    string(1) "1"
    ["created_at"]=>
    string(19) "2016-01-29 00:05:52"
    ["updated_at"]=>
    string(19) "2016-02-11 17:42:15"
    ["city_id"]=>
    int(1)
    ["location_id"]=>
    int(2)
  }
  ["relations":protected]=>
  array(0) {
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["appends":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["casts":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["morphClass":protected]=>
  NULL
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(false)
}

要允许++ ++ ++ i; 三倍增量,i必须返回引用并获取引用。如果它返回一个临时副本,第二个++将递增...临时副本(实际上,临时副本不会绑定++,因此它甚至不会编译)。

答案 1 :(得分:1)

要添加到Emilio的答案,后缀增量会创建一个临时变量并将其设置为1加上要增加的变量,其中前缀增量会增加实际变量,在某些情况下可以提高性能。

答案 2 :(得分:0)

主要原因是在C ++中遵循内置类型的语义,其中前缀增量/减量返回lvalue,后缀1返回rvalue。可以找到详细信息here