trikker
trikker

Reputation: 2719

Operator Overloading for Objects in Multiple Classes

So can you overload operators to handle objects in multiple classes (specifically private members.)

For example if I wanted == to check if a private member in Class A is equal to objects in a vector in Class B.

For example:

bool Book::operator==(const Book& check){
 return(((ISBN1 == check.ISBN1) && (ISBN2 == check.ISBN2) && (ISBN3 == check.ISBN3)
         && (ISBN4 == check.ISBN4)) || (title_ == check.title_));}

Everything in that overload is part of the Book class, however what if I wanted to do something like this.

 if(*this == bookcheckout[i])

with bookcheckout being part of a Library class. The == would fail in trying to compare title_ to a title_ stored in a vector of the Library class.

It's odd because I have the program doing the exact same thing in two different places, but in one place it's working and in the other it isn't.

Answered: had to have the function that the operator was in be a member function of the same class that the operator was a member function of

Upvotes: 0

Views: 9030

Answers (4)

Eugene
Eugene

Reputation: 7268

If you make operator friend or member of one class, it will be able to access its private members. To access privates of both, operator will have to be free standing friend of both.

That is a bit unwieldy, so consider making public interface for interesting things instead.

(suppressed all puns about accessing private parts of multiple entities)

Here is how you can make a very friendly operator, but again, this is not a good solution.

(didn't compile the code)

class B;

class A
{
friend bool operator==(const A&, const B&);
private:
    int private_;
};

class B
{
friend bool operator==(const A&, const B&);
private:
    int private_;
};

bool operator==(const A& a, const B& b)
{
    return a.private_ == b.private_;
}   class B;

This is a better way -- just make public getters and use them in operator.

class A
{
public: 
   int GetPrivate() const { return private_; }
private:
    int private_;
};

class B
{
public:
    int GetPrivate() const { return private_; }
private:
    int private_;
};

bool operator==(const A& a, const B& b)
{
    return a.GetPrivate() == b.GetPrivate();
}  

You also can make operator to be part of one of the classes, if you need privates from it alone. Read up on operator overloading syntax for more.

Upvotes: 2

David Norman
David Norman

Reputation: 19899

You don't specify the type of bookcheckout, so I can't be sure, but I think that your operator== will work without change.

For instance, if the code is:

class Library
{

public:
    const Book & operator[] (int i);
};

Library bookcheckout;

Then your if statement will call the operator== you have without a problem.

Upvotes: 1

Jeff Leonard
Jeff Leonard

Reputation: 3294

You can, but you would need to make either 'bool operator==(A a, B a)' 'friend' of class A if you are using a free function or make class B 'friend' of class A if you implement the comparison operator as a class member function.

You can avoid the friendship requirement by providing a public accessor to the private member in class A

Upvotes: 0

Tamás Szelei
Tamás Szelei

Reputation: 23971

Yes. If you need to access private members, consider providing an appropriate public interface for them OR go for friend class. It is usually better to avoid it though. To handle a specific type, implement operator== with an instance of that type.

Upvotes: 0

Related Questions