Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't a pointer just a reference when you don't dereference it?

Isn't a pointer just a reference when you don't de-reference it?

#include "stdafx.h"
#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>


std::list<int>* user_defined_func( ) {
    std::cout << "BEGIN: user_defined_func" << std::endl;
    std::list<int>* l = new std::list<int>;

    l->push_back(8);
    l->push_back(0);

    std::cout << "END: user_defined_func" << std::endl;

    return l;
}


bool validate_list(std::list<int> &L1)
{

   std::cout << "BEGIN: validate_list" << std::endl;

   std::list<int>::iterator it1 = L1.begin();

   for(; it1 != L1.end(); ++it1)
   {
      if(*it1<= 1){

         std::cout << "Validation failed because an item in the list was less than or equal to 1." << std::endl;
         std::cout << "END: validate_list" << std::endl;
         return false;
       }
   }

   std::cout << "Test passed because all of the items in the list were greater than or equal to 1" << std::endl;
   std::cout << "END: validate_list" << std::endl;
  return true;
}

BOOST_AUTO_TEST_SUITE( test )
BOOST_AUTO_TEST_CASE( test )
{
   std::list<int>* list1 = user_defined_func();
   BOOST_CHECK_PREDICATE( validate_list, (list1) );
}
BOOST_AUTO_TEST_SUITE_END()

In the line,

BOOST_CHECK_PREDICATE( validate_list, (list1) ); 

above, I was told that I can't pass pointer to the function expecting reference. I thought that a pointer (that hasn't been de-referenced) was just an address (i.e. a reference). What am I missing here?

like image 848
leeand00 Avatar asked Feb 24 '10 14:02

leeand00


People also ask

Are references just pointers?

A reference, like a pointer, is an object that you can use to refer indirectly to another object. A reference declaration has essentially the same syntactic structure as a pointer declaration. The difference is that while a pointer declaration uses the * operator, a reference declaration uses the & operator.

What is the difference between a reference and a pointer?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.

Why is this a pointer instead of a reference?

Pointers can be NULL , references cannot be NULL . References are easier to use, const can be used for a reference when we don't want to change value and just need a reference in a function. Pointer used with a * while references used with a & . Use pointers when pointer arithmetic operation are required.

Do you need to dereference a reference?

The explicit dereference is not required by design - that's for convenience. When you use . on a reference the compiler emits code necessary to access the real object - this will often include dereferencing a pointer, but that's done without requiring an explicit dereference in your code.


1 Answers

Pointers and references are similar, but different in several ways:

  1. They have different access syntax. If you have T* a and T& b, you access member variables and functions using a->member and b.member respectively.
  2. Pointers can point to nothing, while references must always point to something. a = 0 is legal, but b = 0 or anything to that effect is not.
  3. Pointers can be "reseated" (i.e. the pointee may be changed) whereas references cannot. a = &b is legal, but int c; b = c; is not (T& b = c is, however -- references may only be set at their initialization). (Thanks Mike D.)
  4. Pointers cannot refer to temporaries, but const references can.
  5. Pointers can point to other pointers, (T**) but references cannot refer to other references (i.e. there's no such thing as T&&, although note that C++0x will be using T&& to define move semantics). As a result, you cannot have arrays of references. (Thanks AshleysBrain)

One might wonder why we have references at all, and not just use pointers all the time (like in C). The reason is because of operator overloading or certain operators.

Consider the assignment operator. What would the function syntax be without references? If it were T* operator=(T* lhs, T rhs) then we'd have to write things like:

int a(1);
&a = 2;

Essentially, references allow us to have functions of l-values without the need for pointer reference and dereference syntax.

like image 134
Peter Alexander Avatar answered Oct 10 '22 02:10

Peter Alexander