Why must my C++ function have different pointer arguments? -
i made c function pointer-passing gives right result if pointers different. instance:
void dotransform(point *pout, const point *pin, transform mat) { pout->x = mat[0][0] * pin->x + mat[1][0] * pin->y + mat[2][0] * pin->z + mat[3][0] * 1.0; pout->y = mat[0][1] * pin->x + mat[1][1] * pin->y + mat[2][1] * pin->z + mat[3][1] * 1.0; pout->z = mat[0][2] * pin->x + mat[1][2] * pin->y + mat[2][2] * pin->z + mat[3][2] * 1.0; }
dotransform()
supposed called this:
//... transform toworldmat; point local; point world; dotransform(&world, &local, toworldmat );
the problem is, in team called this:
point p; dotransform(&p, &p, toworldmat);
it took me 1 week figure out why final output of program changed.
what best style declare kind of function, avoid case? wrong write that?
seeing you're passing transform
object directly (though not const&
: should instead) there reason can't write code so:
point dotransform(point const& pin, transform const& mat) { point pout pout.x = mat[0][0] * pin.x + mat[1][0] * pin.y + mat[2][0] * pin.z + mat[3][0] * 1.0; pout.y = mat[0][1] * pin.x + mat[1][1] * pin.y + mat[2][1] * pin.z + mat[3][1] * 1.0; pout.z = mat[0][2] * pin.x + mat[1][2] * pin.y + mat[2][2] * pin.z + mat[3][2] * 1.0; return pout; }
which allow write code this:
transform toworldmat; point local; point world = dotransform(local, toworldmat);
or this:
point p; p = dotransform(p, toworldmat);
these correct, ideomatic c++ ways write code.
Comments
Post a Comment