Just learning C++, encountered an issue that i’m not really sure why the following is the case.
Doesn’t work
ChildObject in = myObject.getObjFromVector(0);
ChildObject out = myObject.getObjFromVector(1);
doSomething(in.property, out.property); // void doSomething(Thing &in, Thing &out)
where
ChildObject& getObjFromVector(int index)
Works
process(myObject.getObjFromVector(0).property, myObject.getObjFromVector(1).property);
In case your wondering, though i don’t think its relevant, Thing is Mat from opencv. This question however is about why the above works in one way but not the other.
Interestingly Which is more performant, passing a method the entire object, or a property of that object? suggests that both should work, or at least be acceptable. I’m definately doing something wrong though and this might be down to something in openCV i’m not aware off.
edit:
I’m effectively carrying out some image manipulation on the .property value. like
process (Mat &src, Mat &out){
...
pyrDown(src, result, Size(src.cols / 2, src.rows / 2));
...
WHen its not working, i’m not seeing the pyrDown take effect.
This is possibly due to object slicing (I am not familiar with openCV). In the first example:
a copy is made of object referred to by the return value of
getObjFromVector()asinis of typeChildObject, notChildObject&.The second example passes the reference returned directly to the function so no object slicing occurs.