Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pattern for extra pass-by-reference arguments that aren't needed after the function call?

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()?

like image 342
daj Avatar asked Oct 26 '25 22:10

daj


1 Answers

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.

like image 149
James McNellis Avatar answered Oct 28 '25 10:10

James McNellis