Archived

This topic is now archived and is closed to further replies.

consts and stuff

This topic is 5503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


  
    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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites