Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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, what should I pass to function1()?

Thanks!

like image 743
Wei Shi Avatar asked Jan 08 '11 05:01

Wei Shi


1 Answers

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
like image 192
Nawaz Avatar answered Nov 15 '22 04:11

Nawaz