comparing classes

i have 2 objects and i need to know if all the values inside the object are identical for both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std; 

class Hello
{
	public:
	void setx(int x);
	int getx();

	private:
	int m_x;
};

void Hello::setx(int x)
{
	m_x = x;
}

int Hello::getx()
{
	return m_x;
}

int main(int argc, char* argv[])
{

	Hello world;
	int x = 20;
	world.setx(x);
	std::cout << world.getx() << std::endl;

	Hello world2;
	world2.setx(x);
	std::cout << world2.getx() << std::endl;

	if(world == world2)
	{
		std::cout << "objects are equal" << std::endl;
	}
	else
	{
		std::cout << "objects not equal" << std::endl;
	}

	
	cin >> x;

	return 0;
}


i tried it this way it does not compile
what am i missing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace std; 

class Hello
{
	public:
	void setx(int x);
	int getx();

	private:
	int m_x;
};

void Hello::setx(int x)
{
	m_x = x;
}

int Hello::getx()
{
	return m_x;
}

int main(int argc, char* argv[])
{

	Hello *world = new Hello;
	int x = 20;
	world->setx(x);
	std::cout << world->getx() << std::endl;

	Hello *world2 = new Hello;
	world2->setx(x);
	std::cout << world2->getx() << std::endl;

	if(*world == *world2)
	{
		std::cout << "objects are equal" << std::endl;
	}
	else
	{
		std::cout << "objects not equal" << std::endl;
	}
	
	cin >> x;

	return 0;
}
Let me know if it works as you expect.This just tests if the number held by world object is equal with the number from world2 object .
1
2
3
4
5
6
7
8
if(world.getx() == world2.getx())
    {
        std::cout << "objects are equal" << std::endl;
    }
else
    {
        std::cout << "objects not equal" << std::endl;
    }

Last edited on
it does not compile

Does it give you an error message when you try to compile? If so, what is it?

Just from a quick glimpse, I see that you haven't overloaded operator==. You also forget to delete the objects that you allocate with new.
i managed to get it to work by overloading the operator==
i thought there would be a simpler way of comparing all the values in the object.

i have around 50 private variables.

1
2
3
4
5
6
7
8
9
10
11
friend bool operator ==(Hello& a,  Hello& b)
	{
		bool rVal = false;
		if (a.help.compare(b.help) == 0)
			//a.help == b.help)
		   //(a.m_x == b.m_x) && 
		   //(a.m_z == b.m_z)
		   //)
        rVal = true;
		return rVal;
	}
closed account (28poGNh0)
Your variables seems to be identic ,use an array of 50 variables then,instead of 50 variables,compare them when you're everloading operator==,that would help you to decrease your code,
the variables are all of different types.
ptime, int, double, string, bool.
closed account (28poGNh0)
I affraid to say that there is not a way but your own one ,coz I never heard about object comparaison, object can be copied thats easy by a bitwise copying, but I can figur out how It can manage to compare with another

finaly I dont see any harm using your method ,a bunch of codes added so what

hope some expert in this website lending some insights
Just use operator == thats the only way to compare any object? Its just that you know int == int is already overloaded but Hello == Hello is not overloaded yet so you must overload it manually.
Topic archived. No new replies allowed.