2

I was reading a discussion on Why use pointers? and came across an answer by Tooony(most votes) which stated that one reason to use pointers was " when there was no way of passing a variable "by reference" to a function ".

What are the possible cases when we cannot pass a variable by reference?

Any there any other situations where we cannot pass by reference?

Please enlightened me , i cannot think of any situation

Thanks

Community
  • 1
  • 1
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • See this: http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c – chris Dec 19 '12 at 01:35
  • Toony was saying that if you want C compatibility to your API, you have to use pointers because references are not supported in C. – zdan Dec 19 '12 at 01:38
  • 1
    @close-voters: please only vote to close question that you understand. *failure to understand* does not mean that you are competent to vote on the question. it means the opposite. – Cheers and hth. - Alf Dec 19 '12 at 01:52

2 Answers2

3

You ask,

“What are the possible cases when we cannot pass a variable by reference?”

With C++11 there are two main such cases (maybe a few more that I failed to think of):

  • When the function declaration is to be used also for a C language binding.

  • When the object is an array of unknown bounds.

In the latter case one could, conceivably, pass a reference to the array's first item, but then inside the function the code would just have to take the address in order to get back the array-ness, and the formal type would not just be impractical in this way, but also extremely misleading.

Also, to my surprise Visual C++ 11.0 (the compiler shipped with Visual Studio 2012) compiles the following:

#include <iostream>
using namespace std;

void foo( int const (&a)[] )
{
    for( auto p = &a[0]; *p != 0; ++p )
    {
        wcout << *p << endl;
    }
}

extern int const data[];

int main( int argc )
{
    foo( data );
}

int const data[] = {1, 2, 3, 4, 5, 0};

However, MinGW g++ 4.7.1 rejects it, as it should!


One case that did exist in C++03, but no longer exists in C++11, was that in C++03 a temporary object passed to a reference to const had to have an accessible copy constructor. That was because in C++03 the compiler was allowed to make any number of copies. In C++11 the reference has to be bound directly when that's possible.


One case that on closer inspection is a non-case is when you need to express the possibility of “does not refer to an object” like a nullpointer.

For example,

void foo( int const* pValue = 0 ) { if( pValue != 0 ) { ... } }

But there are workarounds, including the one of overloading foo:

void foo( int const& value ) { ... }
void foo() {}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

References must point to initialized memory, that is, they are not allowed to be NULL. If you want to allow parameters that do not refer to an object in a function, then you must use pointers.

However, you seem to have misread what was written:

In C you don't have any support for complex datatypes such as a string. There are also no way of passing a variable "by reference" to a function.

Specifically, there is no such concept as a reference in C. Hence, you must use pointers. In C++, however, there are effectively no places where you cannot use references where you could use pointers in a function argument list.

Yuushi
  • 25,132
  • 7
  • 63
  • 81