some_type& and some_type*

what's the difference between both of them?
some_type& represents the adress of a some_type object whereas some_type* points to the adress of some_type object? I think.


edit: what I said may not be true:
http://stackoverflow.com/questions/620604/difference-between-a-pointer-and-reference-parameter

Another possible usefull topic:
http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c

Seems like theres quite some confusion out there on the difference.
Last edited on
Seems like theres quite some confusion out there on the difference.

yeah, i thought so. i mean, if i write:

int * pointer, p;
pointer = &p;

it would be the same with:

int& pointer, p;
pointer = p;

and the usage is the same except in usage of -> .

i found this article (given by someone):

http://www.parashift.com/c++-faq-lite/references.html

it said:

Unlike a pointer, once a reference is bound to an object, it can not be "reseated" to another object.

but i found that when i wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <conio.h>
using namespace std;

struct test {
	int age;
	void test1 () {
		cout << "this is test\n";
	}
};

int main () {
	test test1;
	test1.age = 9;
	test& test2 = test1;
	cout << test2.age << endl;
	test2.age = 10;
	cout << test2.age << endl << test1.age << endl;
	test2.test1();
	test test3;
	test3.age = 13;
	test2 = test3;
	test2.age = 15;
	cout << test2.age << endl;
	test2.test1();
	getch();
	return 0;
}


is a valid code and it runs...
Last edited on
The context is different. * and & mean different things depending on the context.

1
2
int * pointer, p; // declare a pointer "pointer", and a regular variable
pointer = &p; // store the memory location of p in pointer 


1
2
int& pointer, p; // this doesn't even compile for me.
pointer = p;

You use references like that in function parameters list...
1
2
int function(int &a) // normally "a" would get copied, but in this case it won't be
{ /* ... */ }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int function(int &a)
{
    a = 5;
}

int main()
{
    int b = 32;
    std::cout << b << std::endl;
    function(b);
    std::cout << b << std::endl;

    return 0;
}
Last edited on
yeah, i know there's a lot of difference about using * and & for example, you can't write:

cout << *a << endl;

instead

cout << (&a) << endl;

but what's makes me curious is the declarations of these;

int * p;

and

int& p;

both looks the same for me, i mean even in usage.
* is pointer declaration at line 10
* is dereferencer at line 17
& is "address of" operator at line 14
& is reference declaration at line 25

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

int main(){

	//an integer
	int myInt = 0;
	
	//pointer to integer in stack
	int * pInt;
	
	//pInt now points to myInt
	//pInt = &myInt; means "give me the memory address of myInt and assign it to pInt
	pInt = &myInt;
	
	//now whatever you do with the pointer, is done to myInt
	*pInt = 24;
	cout << "myInt now: " << myInt << endl;
	//above, *pInt = 24; means "assign 24 to the location of pInt"
	//where, myInt sits. so, we're changing myInt's value explicitly.
	
	//this is a reference to myInt
	// int & rMyInt = myInt; means "create an alias for myInt"
	// whatever you do will be don to myInt
	int & rMyInt = myInt; //we have to initialize references immediately
	
	cout << "rMyInt: " << rMyInt << endl;
	cout << "myInt : " << myInt << endl;
	
	//lets play with it
	rMyInt = 88;
	
	cout << "rMyInt: " << rMyInt << endl;
	cout << "myInt : " << myInt << endl;
	
	/*
	output of this program:
	
	myInt now: 24 -->line 18
	
	rMyInt: 24 -->line 27
	myInt : 24 -->line 28
	
	rMyInt: 88 -->line 33
	myInt : 88 -->line 34
	*/
	
return 0;
}
here is a good question;
when should I use references, and when should I use pointers?

http://www.parashift.com/c++-faq-lite/references.html#faq-8.6
but is it true that references can be "reseated"? (my code above) //solved

btw, all i know that even the references used with "private" member it still work like the pointers doing. for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
using namespace std;

class test {
	int age;
public:
	test (int n):
	  age(n)
	  {
	  }

	void test1 () {
		cout << age << endl;
	}
};

int main () {
	test test1 (9);
	test& test2 = test1;
	test2.test1();
	getch();
	return 0;
}


is the same result with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
using namespace std;

class test {
	int age;
public:
	test (int n):
	  age(n)
	  {
	  }

	void test1 () {
		cout << age << endl;
	}
};

int main () {
	test test1 (9);
	test* test2 = &test1;
	test2->test1();
	getch();
	return 0;
}
Last edited on
muratagenc wrote:
when should I use references, and when should I use pointers?
Use references when you want to apply operations (like +, -, =) to them. std::string is an example where you use references. Usually const reference.

Use pointer if you want to modify the content. If you modify a reference you might not know what you're really modifying.

chipp wrote:
but is it true that references can be "reseated"?
No, it's not true. You can however apply the = operator to the reference, but then you only copy the content.
This answers most of the questions posed in this thread, albeit in the context of passing parameters to functions.

http://www.cplusplus.com/articles/z6vU7k9E/
Topic archived. No new replies allowed.