As I see in some code having something like "ClassName&".
For example:

// this demo
// check if a parameter passed to a member function is the object itself
#include <iostream>
using namespace std;

class CDummy {
	public : 
		int isitme ([b]CDummy[u]&[/u] param[/b]);		
};

int CDummy :: isitme ([b]CDummy[u]&[/u] param[/b]) {
	if (&param == this) return true;
	else return false;
}

void main () {
	CDummy a;
	[b]CDummy*[/b] b = &a;
	if (b -> [i]isitme(a)[/i]) {
		cout << "yes, &a is b";
	}
}

I know "ClassName*" is used to declare a pointer to a class instantiation. But what is "ClassName&"? What is the difference between the "&" reference operator and this "&" in "ClassName&"?

Sorry if it is a stupid question!
Thank for any help.

Recommended Answers

Also, see this post and for an article with lots of examples, see this.

(sorry adatapost, I was already in process when you posted)

Jump to Post

All 4 Replies

The notation ClassName& means reference to ClassName.
For example,

int no=10;
  int& ref=no;

In above code snippet, variable ref and no refer to the same int value.

If you want to get a pointer to the object denoted by a ref, you can write:

int *ptr=&ref;    // address of (&) operator.
commented: Thanks! +1

Also, see this post and for an article with lots of examples, see this.

(sorry adatapost, I was already in process when you posted)

commented: You are always helpful +6
commented: Thanks. After reading your example, I remember that my problem related to "passing by ref". +1

Thank all! It's OK now.

* is the dereference operator
* gets the value

& is the opposite. it gets the address instead of the value.

Really, you aught to know this. Its like pointers 101.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.