consts and stuff

Started by
1 comment, last by Draigan 21 years, 4 months ago

  
    Vector3& Center ();
    const Vector3& Center () const;
  
How would the compiler know which version to use? I didn''t previously know that this was possible but I''ve seen Eberly use it in his Magic Software code.
Advertisement
one is for retrieving, the other for setting:
minZ = bOBB.Center().z - bOBB.Extent(2); // retrieving bBox.Center().x = min.x + dX*0.5f; // setting 


the retrieval is done via the const version of the function.

at least that''s the way i use ''em.
If the object being operated-on is a const object, it can only use the second. If the object is non-const, my compiler chooses the first. I don''t believe the compiler can use the return value to determine the function type (IANALL), so I think it just calls the function that corresponds to the type of object.

Example:

  class C{public:	C &foo ()	{		return *this;	}	const C &foo () const	{		return *this;	}};int main (){	C mc;	C &rc = mc.foo (); // calls C &foo ()	const C &crc = mc.foo (); // so does this since mc is non-const	return 0;}  

This topic is closed to new replies.

Advertisement