Thread: References & pointers equivalence

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    20

    References & pointers equivalence

    Can anyone say that these rules below apply (I am not interested how you dereference not for that you must assign initial value when you declare reference) but if they can be used for the same think (say for pointer operations etc):



    int b = 2;

    int& a = b; equivalence with int *a = &b;

    and

    const int& a = b; equivalence with const int *const a = &b;


    So if that applies can anyone provide me a solution how can I use a reference to represent?

    const int *a = &b

    and

    int * const a = &b;


    is this possible ?

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    int& a = b; is equivalent to int * const a = &b; because a reference cannot change what it refers to and a const pointer cannot change what it points to.

    There is no equivalent to int *a = &b; or const int* a = &b; with references.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    20
    Hi

    I don’t agree with the

    int &a=b; is equivalent to int * const a=&b;

    as you respond me

    Lets look the following program:


    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int w = 12;
    	int b = 7;
    
    
    
    	int* const ap = &b;
    
    	//I can change the value
    	*ap = 8;
    
    	//Of course it fails
    	//ap = &w;
    
    	cout << *ap << '\n';
    
    	/* From your responses
    	int &a=b; is equivalent to int * const a=&b;
    	*/
    
    
    	int& ar = b;
    
    	//It MUST be failed but it is NOT ( I can change the reference in order to show in other variable
    	// that means that it is not a const pointer because I cam change the address where it show)
    	ar = w;
    
    	cout << ar << '\n';
    
    	//and also I can change the value
    	ar++;
    
    	cout << ar << '\n';
    
    
    	//So my result is that reference is int& a = b; equivalence with int *a = &b;
    	//Or maybe I do a mistake!!!!!!!!!!!!!
    
    
    	return 0;
    }

    Thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >//It MUST be failed but it is NOT ( I can change the reference in order to show in other variable
    >// that means that it is not a const pointer because I cam change the address where it show)
    >ar = w;
    This is where you're confused. A reference is simply another name for the object it's paired with. You're not re-assigning the reference to another object, you're changing the value of both the reference and the paired object because they're the same thing. Watch:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int a = 123;
      int b = 456;
      int& ra = a;
    
      cout<< a <<'\t'<< b <<'\n';
      ra = b;
      cout<< a <<'\t'<< b <<'\n';
    }
    If ra were actually paired with b, then the value of a would not change. After pairing a reference with an object, any use of the reference is exactly the same as if you replace the reference with the object it's paired with. So in the above code, every use of ra is exactly the same as if you used a instead.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM