Class equality check

Feb 2, 2012 at 11:33am
Is there any way to check if A==B within a function??
1
2
3
4
5
6
7
8
9
10
11
12
Class A{
public: int id;
        string description}

Main()
{
Class A a;
a.id=5;
a.string="hello";
a=b;
Class a B;
}

I'm looking for a code to check if a=b smthing like
1
2
3
if(a==b)
//i know it doesnt works...
cout<<"the same"
Last edited on Feb 2, 2012 at 11:54am
Feb 2, 2012 at 11:36am
Feb 2, 2012 at 11:55am
Sry for editing the post this is what i meant..
Feb 2, 2012 at 12:03pm
You'll need to overload the equality operator, either globally, or internally to the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Internally:
class A
{
    public:
        bool operator == (const A &Ref) const 
        {
            return(this->Member == Ref.GetMember());
        }

        const int GetMember() const
        {
            return(this->Member);
        }

    private:
        int Member;
};

// Globally:
bool operator == (const A &L, const A &R)
{
    return(L.GetMember() == R.GetMember());
}

Note: If you overload an operator globally, be careful.

Wazzak
Last edited on Feb 2, 2012 at 12:05pm
Feb 2, 2012 at 12:36pm
Good idea! thx.. Will this work and for any derived class?
Feb 2, 2012 at 2:39pm
Yeah.

Wazzak
Feb 2, 2012 at 2:44pm
@OFramew: Why did you call the getter function instead of accessing the member directly?
5
6
7
8
        bool operator == (const A &Ref) const 
        {
            return(this->Member == Ref.GetMember()); //why not Ref.Member?
        }
Last edited on Feb 2, 2012 at 2:44pm
Feb 2, 2012 at 2:57pm
To minimize the difference between the internal and global, I suppose.
Feb 2, 2012 at 2:57pm
@LB: No idea. I'm just a simple pencil sharpener. No, seriously, I admit that I didn't know you could do that. How is it possible? Aren't private members, well... private?

Wazzak
Feb 2, 2012 at 3:09pm
Any member function of a class can access all members of that class, regardless of 'privateness'.
Alternatively, your global version would not work that way (unless you make it a 'friend' of the class), because it has no access to private members.
Feb 2, 2012 at 3:17pm
OK, so if I understand this correctly, if an internal function, such as the overloaded equality operator above, references any instance of the same class, I can access the private members of the referenced instance?

I wish I knew this before. Thanks, Gaminic.

Wazzak
Last edited on Feb 2, 2012 at 3:19pm
Feb 2, 2012 at 3:23pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

class myClass {
public:
	bool operator==(const myClass& rhs) { return anInt == rhs.anInt; }
	myClass(int A) : anInt(A) {}
private: 
	int anInt;

};

int main() {
	myClass a(6), b(6), c(10);
	if (a == b) std::cout << "a equals b!" << std::endl;
	if (a == c) std::cout << "a equals c!" << std::endl;
	if (b == c) std::cout << "b equals c!" << std::endl;
	return 0;
}


a equals b!
Feb 2, 2012 at 3:42pm
I understand now, Gaminic. Thanks again :)

Wazzak
Topic archived. No new replies allowed.