C++/class

Advertisement


Question
sir
is there any way by which we can compare two classes
and perform the operation such as comparing and other manipulations.

Answer
The short answer is no, you cannot compare classes.

However I do not think you meant to ask about comparing classes did you? Do you mean comparing objects of different classes? I hope so. If not please post a follow-up question clarifying what you are trying to do - as at that point I think we are into the realms of meta-programming techniques.

If you did mean comparing objectsa of different types then answer would be yes, but would it make sense to do so? Only the author of the classes can say what makes sense for them.

Assuming you have such a case where comparing objects of two different classes makes sense then you can write non-member function operator functions for the task:

   bool operator==( ClassA const & a, ClassB const & b  ); //defined elsewhere
   bool operator==( ClassB const & b, ClassA const & a  )
   {
       return a==b;
   }


And so on.

The other way would be to have each class incorporate an operator that took an object of the other type as a parameter:

   class ClassA
   {
   public:

   ...

       bool operator==( ClassB const & b  );

   ...

   };



   class ClassB
   {
   public:

   ...

       bool operator==( ClassA const & a  );

   ...

   };

Of course this leaves the door open to forgetting to implement all or some of the operations in one of the classes and probably even worse, the possibility that:

   a op b

and

   b op a

are not defined consistently where op is the operator in question (==, !=, < etc). Of course for some operations and type combinations this may be the case, but I am guessing this is probably no so for equality and other comparison operations.

Notice that the non-member function version selected one combination to be the 'master' that implements the logic (commented as defined elsewhere) and the other function just reverses the order of the parameters (OK for equality).

In fact taken to its extreme you only need operator== and operator< (and/or operator>) as:

   a != b => b != a : !(a==b) => !(b==a)
   a < b => b > a
   b < a => a > b
   a <= b => b >= a : (a==b || a < b ) => (b==a || b > a)
   b <= a => a >= b : (b==a || b < a ) => (a==b || a > b)

Sorry if I got some of those wrong they came straight out of my head and into the answer! Hopefully you will get the idea and can check the transformations more carefully yourself.

In fact doing all these by hand can get quite tedious. This is why the Boost libraries (http://www.boost.org/) have an operator library that helps you define additional operators for your types based on a few core operations. See http://www.boost.org/libs/utility/operators.htm and the two argument forms in particular at http://www.boost.org/libs/utility/operators.htm#two_arg.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.