Ruggero Turra
Ruggero Turra

Reputation: 17670

when i need pass by pointer (and not by reference)

Can you give me an example when I can't pass argument by reference and I need to use pointer. I've found an example, but I'm not sure.

Suppose you have a class D derived from the base class B. You need pointer if you want do so:

void function(B* b){...}
int main{
  D* d;
  function(d);
}

Upvotes: 0

Views: 424

Answers (8)

Puppy
Puppy

Reputation: 146910

Typically, you use pointers for one of two things:

  • Reassignability - you can't rebind a reference.
  • Null pointers - there's no such thing as a null reference.

If your intended use case does not need either of those two properties, use a reference. Else, use a pointer.

Upvotes: 1

Michael
Michael

Reputation: 1022

Another case: if the thing you're passing is the last argument before varargs:

void fn1(A &a, ...); // Uh oh
void fn2(A *a, ...); // Good

I don't know if this is required by the standard, or is just a bug in the implementation of the C++ compiler I use.

Upvotes: 1

Patrick
Patrick

Reputation: 23619

I think that if you want to pass a function, you have to pass it by pointer. I don't see how you can pass the function by reference.

For example, take the following function:

#include <iostream>
#include "math.h"

void myfun (double value, size_t nofloops, double (*function)(double))
   {
   std::cout << value << std::endl;
   for (size_t i=0;i<nofloops;++i)
      {
      value = function(value);
      std::cout << value << std::endl;
      }
   std::cout << "------------------" << std::endl;
   }

void main()
   {
   myfun(100,10,sin);
   myfun(100,10,cos);
   myfun(100,10,sqrt);
   }

The function in this small utility executes the given function a number of times, taking the result as input in the next iteration. I can't see how you can pass the function by reference.

Upvotes: -2

Jacob Nelson
Jacob Nelson

Reputation: 3006

http://www.daniweb.com/forums/thread216353.html

Singly linked lists example were pointers and pointer of pointers are used as function parameters.

Upvotes: 0

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74340

If you want to allow the lack of an object, you need to use pointers:

// This allows DoSomething to receive pointers to NULL, which cannot
// be done with references
void DoSomething(Something *pSomething)
{
  if (pSomething)
  {
    ...
  }
}

int main()
{
  Something *pSomething=NULL;

  DoSomething(pSomething);
}

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145204

If there is a coding guideline (like Google's) that says to use pointer arguments, then that's what you do.

Otherwise, only declare your own function with pointer formal argument when

  • a nullpointer is a valid & meaningful actual argument, or

  • the actual argument is most naturally pointer already, or

  • you're going to store that pointer somewhere.

Possibly more cases, but I think you get the drift: when you have a choice (no coding guideline saying otherwise), prefer references.

Cheers & hth.,

Upvotes: 6

pm100
pm100

Reputation: 50110

the only reason is if you need to pass null. I.e you want to call the function saying 'I haven't got one of those'

Upvotes: -1

Andr&#233; Caron
Andr&#233; Caron

Reputation: 45239

The single time where you can not use a reference and must use a pointer is if you allow the concept of "no argument" by passing a null pointer.

However, you might want to use pointers as arguments when you are actually storing a pointer to whatever was passed. Most C++ developpers will notice that you aren't using a reference and pay special attention to what your documentation says.

Upvotes: 6

Related Questions