3

Is there a preferred pattern for defining throwaway variables in C++ to pass to functions which take arguments by reference? One example of this is in using openCV's minmaxLoc function:

void minMaxLoc(const Mat& src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, const Mat& mask=Mat())

The function changes the value of minVal, maxVal, minLoc, maxLoc to correspond to the min/max intensity values and locations.

I could declare dummy variables for all of the parameters, but I really only need maxLoc. Is there a preferred C++ pattern such that I only declare maxLoc and don't clutter up my code with extra declarations of variables that I'm defining for the sake of calling minMaxLoc()?

daj
  • 6,962
  • 9
  • 45
  • 79

2 Answers2

3

In this specific case, you can pass nullptr for any out parameters whose results you do not need. See the OpenCV documentation (it's always good to read the documentation).

If the parameter is not optional, you need to declare local variables and pass their addresses. If you don't want this to clutter your code, write a helper function to encapsulate the clutter.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • should've caught that w/ respect to opencv, thanks. Guess the onus of allowing for this is on the function definition. – daj Mar 05 '12 at 19:47
1
  1. If you have control on writing the function you could use variable length arguments Passing variable number of arguments around

  2. If you don't then one idea is to write ease-of-use wrappers that create the dummies for you and just call the wrappers

Community
  • 1
  • 1
Sid
  • 7,511
  • 2
  • 28
  • 41
  • Variadic functions should be avoided in C++ because they are not type safe. (Variadic function templates will solve this issue, eventually, but not all compilers support variadic templates yet.) – James McNellis Mar 05 '12 at 19:59