## Monday, 20 February 2012

In the previous post we looked at the mandated member operators, for this part I'm going to look at the different types of operators we can generate and provide examples of each.

For ease I will split this section into relational operators and arithmetic operators. We can represent the relational operators using the following diagram

These operators will always return a boolean value which will be the result of some form of comparison.

Arithmetic operators will return an object of some type which depending upon the context may be either the same object or a new type of object.  Later we will also add the += style operators which will mutate the current object rather than returning a value.
Equality Operators
In the case of the Vec3 class there are only two equality operator which make sense to implement, these are the == and != operators. Also we must ensure that we are doing correct comparison as we are storing floating point values. ( see here for an in depth discussion of floating point comparison ). For this example I'm going to generate a very simple FCompare macro which will be used to do a comparison via a simple epsilon error value.

//----------------------------------------------------------------------------------------------------------------------
/// @brief define EPSILON for floating point comparison
//----------------------------------------------------------------------------------------------------------------------
#ifndef EPSILON
const float EPSILON = 0.001f;
#endif

//----------------------------------------------------------------------------------------------------------------------
/// @brief FCompare macro used for floating point comparison functions
//----------------------------------------------------------------------------------------------------------------------
#define FCompare(a,b) \
( ((a)-EPSILON)<(b) && ((a)+EPSILON)>(b) )

In the first example I will produce a "Member operator" for the equality operator, first in the .h file we declare the operator
bool Vec3::operator==(const Vec3 &_rhs)
{
return FCompare(m_x,_rhs.m_x) &&
FCompare(m_y,_rhs.m_y) &&
FCompare(m_z,_rhs.m_z);
}

As you can see in this case we do a component wise FCompare with the current object and the rhs value. We can test this using the following code from the previous examples
if(b==c)
std::cout<<"b==c is true\n";
else
std::cout<<"b!=c \n";
if(a==fromFloat)
std::cout<<"a==fromFloat\n";
else
std::cout<<"a!=fromFloat\n";

Which gives the following output
testing equality operators
b==c is true
a!=fromFloat

To generate the != method we do the following

/// in .h
/// @brief != operator
bool operator!=(const Vec3 &_rhs);
/// in .cpp
bool Vec3::operator!=(const Vec3 &_rhs)
{
return !(*this==_rhs);
}

For the free function operator we have to write some additional code again to allow the free function access to the attributes. In this case we are going to write a method called isEqual ( note that my coding standard says this kind of method should be formed as a question as it returns a bool)
/// @brief function to test equality
bool isEqual(const Vec3 &_rhs) const;

The actual code for this class method will use the same FCompare macro as above and looks like this
bool Vec3::isEqual(const Vec3 &_rhs) const
{
return FCompare(m_x,_rhs.m_x) &&
FCompare(m_y,_rhs.m_y) &&
FCompare(m_z,_rhs.m_z);
}

The free function definitions of the == and != methods are now declared outside of the class but still in the .h file as follows
/// @brief free operator for equality testing
bool operator ==(const Vec3 &_lhs, const Vec3 &_rhs);
/// @brief free operator for equality testing
bool operator !=(const Vec3 &_lhs, const Vec3 &_rhs);

Finally we write the main function code, not that as they are no longer class methods it is no longer valid to mark the methods as const (in fact you get the following error with g++ error: non-member function 'bool operator==(const Vec3&, const Vec3&)' cannot have cv-qualifier )
bool operator ==(const Vec3 &_lhs, const Vec3 &_rhs) const
{
return _lhs.isEqual(_rhs);
}

bool operator !=(const Vec3 &_lhs, const Vec3 &_rhs)
{
return !_lhs.isEqual(_rhs);
}


1. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. Python Training in Chennai

2. This information is impressive; I am inspired by your post writing style & how continuously you describe this topic.... Java Training in Chennai | Python Training in Chennai

3. This comment has been removed by the author.

4. Thank you for excellent article.You made an article that is interesting.
AWS Solutions Architect courses in Bangalore with certifications.
https://onlineidealab.com/aws-training-in-bangalore/

5. This comment has been removed by the author.

6. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

7. Taldeen is one of the best plastic manufacturing company in Saudi Arabia. They are manufacturing Handling Solutions Plastic products like Plastic Pallets and plastic crates. Here is the link of the product
Handling Solutions
Plastic Pallets
GrueBleen is one of the Branding and Marketing agency Based in Riyadh- Saudi Arabia. The main functions of GrueBleen is Advertising, Branding, Marketing, Office Branding, Exhibition Management and Digital Marketing. Visit the below link to know more about GrueBleen Creative Club.
Agriculture Solutions – Taldeen is a plastic manufacturing company in Saudi Arabia. They are manufacturing agricultural plastic products like greenhouse cover and hay cover. Visit the below link to know more details
Agriculture Solutions
Greenhouse Cover
GrueBleen – One of the best social media marketing agency in Riyadh- Saudi Arabia. Visit here for the all service details of GrueBleen.
Social Media Marketing Agency | Social Media Agency In Saudi Arabia | Social Media Agency In Riyadh | Social Media Agency in Jeddah |

8. Right here is the right website for anyone who really wants to find out about this topic. You know so much its almost hard to argue with you (not that I personally would want to…HaHa). You definitely put a brand new spin on a topic that's been written about for ages. Wonderful stuff, just wonderful!

9. Ogen Infosystem is one of the best Website Designing and PPC Company in Delhi, India. Here you will well experience web designers and developers to provide a great website for your business. Our PPC Experts generate leads to your business products at an affordable price.
PPC Company in Delhi

10. I am glad to post a worthy article about the German Language Course and IELTS Coaching from KCR consultants, this may change your career growth and language skill.
KCR CONSULTANTS
KCR CONSULTANTS

11. Thanks for posting the best information and the blog is very helpful.digital marketing institute in hyderabad

12. I truly appreciate just perusing the entirety of your weblogs. Just needed to educate you that you have individuals like me who value your work. Unquestionably an extraordinary post. Caps off to you! The data that you have given is exceptionally useful.data science training in chennai

13. Thanks for sharing I really loved this article the way you presented is really amazing this helped me to gain a lot of knowledge
Best Software training institues

14. cool stuff you have and you keep overhaul every one of us data science course in mysore

15. Infycle Technology, No.1 Software Training Institute in Chennai, afford best Data Science training in Chennai and also provide technical courses like Oracle, Java, Big data, AWS, Python, DevOps, Digital Marketing, Selenium Testing, etc., and we also provide the best training from the best technical trainers and after completion of training students will be able to crack the jobs on top MNC’s. for more information call 7504633633.

16. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing.data scientist course in warangal

17. Infycle Technologies, the No.1 software training and job placement institute offers the affordable Oracle training in Chennai for techies, students and freshers. Along with the Oracle, other top demanding software courses such as Python, Power BI, Digital Marketing, Data Science, Big Data, Java, AWS, Machine Learning, will be taught here. Dial 7502633633 to get more info and a free demo.

18. Jump into a great learning experience of learning AWS Training in Chennai from Infycle Technologies, the finest software training Institute in Chennai. Also, a proper place to learn other technical courses like Cyber Security, Graphic Design and Animation, Block Security, Java, Cyber Security, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., and here we provide well-experienced trainers with excellent training to the freshers. And we also provide 100+ Live Practical Sessions with Real-Time scenarios which helps the students in learning the technical stuff easily and they are able to crack interviews in top MNC’s with an amazing package. for more queries approach us on 7504633633, 7502633633.

19. I'm genuinely getting a charge out of scrutinizing your richly formed articles. Apparently you consume a huge load of energy and time on your blog. I have bookmarked it and I am expecting scrutinizing new articles. Continue to do amazing.data science course in ghaziabad

20. I truly appreciate just perusing the entirety of your weblogs. Just needed to educate you that you have individuals like me who value your work. Unquestionably an extraordinary post. Caps off to you! The data that you have given is exceptionally useful.data scientist course in bhopal