Mixing objects from different classes?

This is a discussion on Mixing objects from different classes? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to overload the relational operator and compare objects from different classes. say for instance I have: ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    Mixing objects from different classes?

    Hi, I am trying to overload the relational operator and compare objects from different classes.

    say for instance I have:

    class A{
    int x;
    };

    class B : public A{
    int y;
    };

    I would like to have code in main that looks like A < B, where x and y are being compared.

    I don't know which class to put the operator function in.

    I think I need a Programmer Defined Conversion Function? but I am not sure how to use one. Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    // One of many solutions is this


    class A {
    int x;
    virtual int getFromChild () {return x;}
    public:
    int operator < (A& right) {return this -> x < right.getFromChild ();}
    A (int val = 0) {x = val;}
    };


    class B : public A {
    int y;
    int getFromChild () {return y;}
    public:
    B (int val = 0) {y = val;}
    };


    #include <stdio.h>
    void main () {
    B right (2);
    A left (1);
    if (left < right) printf ("it works!\n");
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In general terms the < operator has a left hand side and a right hand side. You need to overload the operator for all possible combinations you want to use it in. That is,if A and B are different classes then A < B is different than B < A to the compiler. You can define what < means in any way you wish, but following traditional guidelines is recommended else you end up causing more confusion than it's worth. Here's a simple example of a straightforward overloading process for < using two simple classes.


    Code:
    struct  A
    {
       int Adata;
       bool operator < (A &);
       bool operator < (B &);
    };
    
    struct B
    {
      int Bdata;
      bool operator < (B &);
      bool operator < (A &);
    };
    
    bool operator < (A & rhs)
    {
        Adata < rhs.Adata ? return true : return false;
    }
    
    bool operator < (B & rhs)
    {
        Adata < rhs.Bdata ? return true : return false;
    }
    
    bool operator < (B & rhs)
    {
       Bdata < rhs.Bdata ? return true : return false;
    }
    
    bool operator < (A & rhs)
    {
       Bdata < rhs.Adata ? return true : return false;
    }

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    bool operator < (A & rhs)
    {
        Adata < rhs.Adata ? return true : return false;
    }
    // why not write this instead
    bool operator < (A & rhs)
    {
        return Adata < rhs.Adata;
    }
    Seems a lot clearer to me at least.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I guess because I don't like to use return statements that have to be calculated first. I like to see exactly what I am returning. To me that's easier to understand. return Adata < rhs.Adata; etc. certainly works if you prefer that syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 09:02 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 07:48 PM
  3. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 03:19 PM
  4. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21