0

Maybe this question has been asked before but I just do not know how this question should be asked

I have the following code. I do not know what is the rationale for the parameter being passed in as const modifiers and then passed into internal constructor.

bool somefunction(const cv::Vec4i& l1_c, const cv::Vec4i& l2_c)
{
   cv::Vec4i l1(l1_c), l2(l2_c);    // why code it in such a manner? 
                                    // what the advantages?
   ...... other codes but l1 and l2 
          is used as values ........
 }

Isn't it more simple to use as cv::Vec4i& l1c as follows

bool somefunction(cv::Vec4i& l1_c, cv::Vec4i& l2_c)
{

      ...... other codes and l1_c and l2_c 
      is used as values .........
 }

Regards

user1538798
  • 1,075
  • 3
  • 17
  • 42
  • 1
    You don't want the arguments to modified inside the function right? – Inian Apr 02 '19 at 07:49
  • 6
    I suggest reading Item 3 ("Use `const` whenever possible") in Scott Meyer's Effective C++. – lubgr Apr 02 '19 at 07:50
  • 1
    Viable alternative would be `bool somefunction(cv::Vec4i l1_c, cv::Vec4i l2_c)` as you copy anyway. – Jarod42 Apr 02 '19 at 07:56
  • Thanks for the answer/comment given. Just to make the question clearer.. the variable is not being changed but it's value is being used. – user1538798 Apr 02 '19 at 07:57

1 Answers1

0

There is a fundamental difference between these two, namely that you cannot change the constant version. Furthermore, you can pass temporaries to the constant version, but not to the non-constant version.

Thomas Lang
  • 770
  • 1
  • 7
  • 17