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!=fromFloatTo 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); }
ReplyDeleteThis is my first visit to your blog, keep writing more content.
Hadoop Training in Chennai
Android Training in Chennai
Selenium Training in Chennai
Digital Marketing Course in Chennai
JAVA Training in Chennai
German Classes in chennai
java training in adyar
This comment has been removed by the author.
ReplyDeleteNice post...Thanks for sharing..
ReplyDeletePython training in Chennai/
Python training in OMR/
Python training in Velachery/
Python certification training in Chennai/
Python training fees in Chennai/
Python training with placement in Chennai/
Python training in Chennai with Placement/
Python course in Chennai/
Python Certification course in Chennai/
Python online training in Chennai/
Python training in Chennai Quora/
Best Python Training in Chennai/
Best Python training in OMR/
Best Python training in Velachery/
Best Python course in Chennai/
This comment has been removed by the author.
ReplyDeleteI just wanted to let you know that the piece you shared with us is pretty unique and authentic. I am going to keep browsing this blog.
ReplyDeleteAndroid Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery
Great Article
ReplyDeleteFinal Year Projects in Python
Python Training in Chennai
FInal Year Project Centers in Chennai
Python Training in Chennai
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!
ReplyDeletetech gadget
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.
ReplyDeletePPC Company in Delhi
thanks for sharing this information with us
ReplyDeletefull stack developer course
full stack developer course in Bangalore
Thanks for sharing I really loved this article the way you presented is really amazing this helped me to gain a lot of knowledge
ReplyDeleteBest Software training institues
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.
ReplyDeleteEvernote Premium Crack is an extreme answer for make, make due, and sort out notes, updates, plans for the day, and substantially more in an easy! Thums Up Evernote Crack
ReplyDeleteMay God bless you with all of the wonderful desires of your heart and bless you to keep the many blessings that you already have.GIRLFRIEND BIRTHDAY WISHES
ReplyDeleteI like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
ReplyDeleteVery interesting blog.
ZModeler Crack/
SEO PowerSuite Crack/
BitComet Crack/
NTLite Crack/
ScreenHunter Pro Crack/