What does “Class* &cls” mean in C++'s function definition?

前端 未结 4 2269
后悔当初
后悔当初 2020-12-15 01:45

I know Class *cls is a pointer, and Class &cls takes the address, but what is

void fucction1( Class *&cls)

If I have Class c

4条回答
  •  醉梦人生
    2020-12-15 02:16

    Besides, what James explained in his response, let me add one more important point to it.

    While you can write Class* & (reference to pointer) which is perfectly valid in C++ only, you cannot write Class& * (pointer to reference), as you cannot have a pointer to a reference to any type. In C++, pointer to reference is illegal.

    §8.3.2/4 from the language specification reads,

    There shall be no references to references, no arrays of references, and no pointers to references.


    If I have Class c, what should I pass to function1()?

    You can write your calling code like this:

    Class *ptrClass;
    
    //your code; may be you want to initialize ptrClass;
    
    function1(ptrClass);
    
    //if you change the value of the pointer (i.e ptrClass) in function1(),
    //that value will be reflected here!
    //your code
    

提交回复
热议问题