diff between "CDummy&" and "CDummy &param"

Hi, I have seen the tutorial of Classes II of C++.

What is the difference between "CDummy&" and "CDummy &param"
In what situation should I use which.

Thanks.
You mean, for instance, what the difference between this:
void f(T &);
and this:
void f(T &a);
?
For the compiler, there's no difference. For someone using the function, the latter may be more useful if the parameter names are meaningful and provide insight as to what they are. The latter needs, however, to be kept in sync with the function definition, so you can't have
void f(T &a);
and then
1
2
3
void f(T &b){
//...
}
(At least I don't think so.)
Yeah, the names can be different; the compiler won't care.
What I mean is, from the example in the tutorial,


#include <iostream>
using namespace std;

class CDummy {
public:
int isitme (CDummy& param);
};

int CDummy::isitme (CDummy& param)
{
if (&param == this) return true;
else return false;
}

int main () {
CDummy a;
CDummy* b = &a;
if ( b->isitme(a) )
cout << "yes, &a is b";
return 0;
}



The a in

if ( b->isitme(a) )

is a CDummy object

and the parameter param in

int CDummy::isitme (CDummy& param)

is a pointer (Am I incorrect here? Please point me out.)

So this is a pointer (this) comparing to an object (&param) in

if (&param == this) return true;


This is something I don't understand.
They seem to be not not of the same type, though the program works of course. May I be pointed out where my understanding is wrong? Thanks.
A reference is not a pointer because whereas pointers can be NULL, you have to go to great lengths to force a NULL reference.

The compiler automatically "promotes" (for lack of a better word at the moment) a to a reference to a when it calls the function.

The whole point of references (or at least most of it) is to allow the user to access the variable using member syntax instead of pointer syntax.

ie, this is ugly:

1
2
string* s = new string( "Hello World!" );
cout << "The 3rd character in s is " << s->operator[]( 3 ) << endl;
If an object is automatically promoted to a reference, does it mean the parameter (the object passed to a function) will be a pointer and should be denoted by &param? For example in the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Vector::functionA() {
   ...
};

void Vector::print(Vector &another) {   // 2. here the parameter is a pointer
                                                             //     such that the another is the object
   cout << &another << endl;
   another.functionA();
};

int main() {
   ...
   Vector v1;
   v1.print(v1);   // 1. here I suppose to give print a reference
   ...
}


So I am wondering the reason why parameter, I give as a reference, when passed to the function becomes a pointer.
Internally it is passed by pointer, but the idea is that references allow you to access the data as if it were not a pointer.

So to continue my above example,

1
2
3
4
string s( "Hello World" );
string& ref_s( s );

cout << "The 3rd character in ref_s is " << ref_s[ 3 ] << endl;

Topic archived. No new replies allowed.