Alternatively you can add the operator overload as non-member:
#include <iostream> using namespace std; class Animal{ public: int weight; }; static bool operator!=(const Animal& a1, const Animal& a2) { return a1.weight != a2.weight; } int main(){ Animal x, y; x.weight = 33; y.weight = 3; if(x != y) { cout << "Not equal weight" << endl; } else { cout << "Equal weight" << endl; } }
Related Posts:
- What are the differences between a pointer variable and a reference variable in C++?
- C++ — expected primary-expression before ‘ ‘
- Return array in a function
- What does “dereferencing” a pointer mean?
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- What does “dereferencing” a pointer mean?
- What is a char*?
- What exactly is nullptr?
- What is a dangling pointer?
- Error with multiple definitions of function
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- C++ – No matching member function for call to ‘push_back’
- What is uintptr_t data type
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Pointer to incomplete class type is not allowed
- Passing a 2D array to a C++ function
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to access the contents of a vector from a pointer to the vector in C++?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- C++ String Variable Declaration
- What does int & mean
- C++: Expression must have a constant value when declaring array inside function
- Sorting Linked List C++ with pointers
- Delete 2D array C++
- Is the sizeof(some pointer) always equal to four?
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- How to initialize a vector of pointers
- What is the use of intptr_t?
- C++ – Assigning null to a std::string
- How do I get the type of a variable?
- error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- Reference to non-static member function must be called
- Why use pointers?
- What’s the difference between * and & in C?
- C++ pass an array by reference
- C++ Global variable declaration
- Function stoi not declared
- C++ Array of pointers: delete or delete []?
- Why should I use a pointer rather than the object itself?
- Function stoi not declared
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- Initializing pointers in C++
- Difference between function arguments declared with & and * in C++
- Is C++ Array passed by reference or by pointer?
- Difference between the int * i and int** i
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- clearing a vector of pointers [duplicate]
- C++ Swapping Pointers
- Array of Linked Lists C++
- Differences between unique_ptr and shared_ptr
- C++ equivalent of Java’s toString?
- Returning an empty string : efficient way in c++
- invalid conversion from ‘const char*’ to ‘char*’
- Evaluate a string with a switch in C++ [duplicate]
- Expected initializer before function name
- Inheriting constructors
- Pass a vector by reference C++
- lvalue required as left operand of assignment – Array
- Why do we use volatile keyword? [duplicate]
- C++ round a double up to 2 decimal places
- Windows 7 exception code: 0xc0000409
- undefined reference to ‘std::cout’
- warning: control may reach end of non-void function
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- Reading integers from file and store them in array C++ [closed]
- Uninitialised value was created by a stack allocation
- C++ Image Processing Libraries
- Printing the correct number of decimal points with cout
- error C2011: ” : ‘class’ type redefinition
- C++ “Access violation reading location” Error
- Getting error: ISO C++ forbids declaration of with no type
- IntelliSense: the object has type qualifiers that are not compatible with the member function
- codingbat-like site for C++
- extra qualification error in C++
- What is the meaning of a C++ Wrapper Class?
- terminate called after throwing an instance of ‘std::out_of_range’ what(): basic_string::substr
- error C2106: ‘=’ : left operand must be l-value
- What does it mean that “a declaration shadows a parameter”?
- multiple definitions error in c++ and solution to solve this issue
- Struct inheritance in C++
- How do I simply compare characters in C++?
- How to Check the Version of my gcc?
- The tilde operator in C
- Game Engines that use PHP for scripting?
- Direct way of computing clockwise angle between 2 vectors
- Member function with static linkage
- C++ equivalent of java’s instanceof
- Conversion from ‘myItem*’ to non-scalar type ‘myItem’ requested
I have read so many posts about the blogger lovers however this post is really a good piece of writing, keep it up