Isn't a pointer a link if you don't cast it?

Is a pointer a non-reference if you are not referencing 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 line

BOOST_CHECK_PREDICATE( validate_list, (list1) ); 

above, I was told that I cannot pass a pointer to a function that is waiting for a link. I thought the pointer (which was not canceled) was just an address (i.e. Link). What am I missing here?

+5
source share
5 answers

Is not just a pointer a pointer when you are not referencing it?

No, the pointer contains a value that is interpreted as a memory address. (Is it containing a value that is actually a valid memory address is another question)

- , .

int i = 5;
int* p = &i; // The value in p is the address that i is stored at.
int& r = i;  // The value in r is 5, because r is an alias of i.
             //   Whenever you use r, it as if you typed i, and vice versa.
             //   (In this scope, anyway).

int sum = i + r; // Identical to i + i or r + i or r + r.

Edit:

list1 , ...?

. , , :

std::list<int>* list1 = user_defined_func();
std::list<int>& list1ref = *list1;
BOOST_CHECK_PREDICATE( validate_list, list1ref );
delete list1;

, :

std::list<int>* list1 = user_defined_func();
BOOST_CHECK_PREDICATE( validate_list, *list1 );
delete list1;

( L1. {something} L1 β†’ {something}):

bool validate_list(std::list<int>* L1) { ... }
+4

, -:

  • . T* a T& b, - a->member b.member .
  • , -. a = 0 , b = 0 - .
  • "" (.. ), . a = &b , int c; b = c; is not (T& b = c is, - ). ( Mike D.)
  • , const .
  • (T**), (.. , T&&, , ++ 0x T&& ). . ( AshleysBrain)

, , (, C). .

. ? T* operator=(T* lhs, T rhs), , :

int a(1);
&a = 2;

, l- .

+13

- ( ), ++ . , C " ", , C , ++.

+6

, , .

, , , .. .

, , , .

,

BOOST_CHECK_PREDICATE( validate_list, (*list1) );

, , :)

, - ++?

+2

, . :

  • , .
  • NULL, .
+1

All Articles