Archived

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

Class Private Member Comparison :: C++

This topic is 5588 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

Hi. I find that I often need to make a comparison between a private variable of some class with another variable outside of the class. I would like to know the best way to make the comparison. Here are two examples. ----- MyClass mCls; int nNum = 1; // Assuming GetNum() returns an int private variable of MyClass. if (nNum == mClas.GetNum()) ... ----- Here is the second possible algorithm. ----- MyClass mCls; int nNum = 1; // Assuming GetNum() accepts a const reference to an int variable. // GetNum() returns a bool based on the comparison. if (GetNum(nNum)) ... ----- Which if any of the example is preferred? I am also aware there are other possible solutions such as the use of a pointer. However, I believe using reference is faster and is more optimized than a pointer. In general, I use const reference when possible and pointer otherwise. Thanks, Kuphryn

Share this post


Link to post
Share on other sites
quote:
Original post by kuphryn
I would like to know the best way to make the comparison.

It''s not possible to tell with the hypothetical scenario you''ve provided.
quote:

// GetNum() returns a bool based on the comparison.

Then GetNum does not do what its name suggests it does. It would be better to call the function IsNumEqualTo or something like that, but that still might not be a good solution to your problem (see above).
quote:

However, I believe using reference is faster and is more optimized than a pointer.

For what you''re doing a reference will be implemented as a pointer. However, the question here is why are you engaging in mindless premature micro-optimisation when you haven''t even decided how the code should work? The relative performance characteristics of your suggested solutions will merely provide a meaningless distraction from you actually finding the *right* solution.

Share this post


Link to post
Share on other sites
I have to agree with SabreMan.
First make it work, then make it fast.

  
bool compare(const int x);
bool compare(const int &x);
bool compare(const int *x);

It this case, Microsoft Visual C++ 6.0 debug build generates the same code for pass by value, pass by reference and pass as pointer. The only difference is in how the address of x is calculated before being pushed onto the stack.
Some people prefer to pass as a pointer if the function will modify the argument, otherwise pass by reference.

Share this post


Link to post
Share on other sites
quote:
kuphryn
Hi.

I find that I often need to make a comparison between a private variable of some class with another variable outside of the class. I would like to know the best way to make the comparison.

Its your own programming style. no one can teach you your style...

Share this post


Link to post
Share on other sites