What is the meaning of & in c++?

前端 未结 6 806
走了就别回头了
走了就别回头了 2020-12-03 16:50

I want to know the meaning of & in the example below:

class1 &class1::instance(){

///something to do

}
6条回答
  •  情书的邮戳
    2020-12-03 16:55

    This means your method returns a reference to a method1 object. A reference is just like a pointer in that it refers to the object rather than being a copy of it, but the difference with a pointer is that references:

    • can never be undefined / NULL
    • you can't do pointer arithmetic with them

    So they are a sort of light, safer version of pointers.

提交回复
热议问题