overloading == and static int trouble

im trying to compare to objects of the same class by overloading the == operator. inside the overloading function i compare the two objects unique id's. The unique id's are static integers that increment everytime an object is made.

The problemm I am having is that the object that invokes the overload, it's unique id gets updated inside the overload function.

Why is this happening and how would I correct it?

here is the overload function inside the class
1
2
3
4
5
6
7
8
9
10
11
12
// overload ==
bool Team::operator ==(Team obj){

 Team t=*this;
 std::cout<<t.uniqueID<<std::endl;
 std::cout<<obj.uniqueID<<std::endl;
 if(t.uniqueID==obj.uniqueID){
  return true;
 }else{
  return false;
 }
}

and here is main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc,char *argv[]){

 Team t1("My Team 1");
 

 Team t2("My Team 2");
 
 
 if(t1==t2){
  cout<<"True"<<endl;
 }
 else{
  cout<<"False"<<endl;
 }
 return 0;
}
im still abiggener but i will try to help...its the first time for me to see arguments iside the main .
and im very ceruose about it..
youv said that the data to be updated ,u can put "const" after the function wich will initilize the variables ..by that you will be able to manipulate with them but not changing them.

Uniqueid is what, exactly? And also, if it's private or protected, is your operator a friend?

Try declaring that your operator needs overloading like this:
bool operator==(const Team lhs, const Team rhs)

But remember to declare it as a friend as well inside your class.

I hope this helps, as my sleep last night wasn't perfect.

-Albatross
Last edited on
I tried doing what you said Albatross but the uniqueId's are still giving the value 2 for each object. It should be 1 for obj and 2 for obj2:

here is the class in full:

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

class Team{
 char *teamName;
 static int uniqueID;

public:
 Team(){
   teamName="";
   uniqueID++;
 }
 
 Team(char *name){
  teamName=name;
  uniqueID++;
 }
 
 char *getTeamName(){
  return teamName; 
 }

 friend bool operator==(const Team obj,const Team obj2);

};

int Team::uniqueID=0;

bool operator ==(const Team obj,const Team obj2){

 std::cout<<obj.uniqueID<<std::endl;
 std::cout<<obj2.uniqueID<<std::endl;
 if(obj.uniqueID==obj2.uniqueID){
  return true;
 }else{
  return false;
 }
}


@ARWA the arguments inside main can be used when you pass values at the command prompt, the first argument is the amount of arguments passed, it is always atleast 1 (i think) even if you dont pass anything, and the first one is the path of the programs exe. the second is an array of all the arguements passed.
hmm a simple work around was assigning an int called id, to the static uniqueID in the constructor and then comparing the int id instead of the static uniqueID.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Team{
 char *teamName;
 static int uniqueID;
 // this will be assigned to uniqueID's value
 int id;
public:
 Team(){
   teamName="";
   uniqueID++;
   // assign id to uniqueID
   id=uniqueID;
 }
 
 Team(char *name){
  teamName=name;
  uniqueID++;
  id=uniqueID;
 }
 


1
2
3
4
5
6
7
8
9
10
11
12
13
// rewritten == overload to compare non static int id

bool Team::operator ==(Team obj){
 std::cout<<id<<std::endl;
 std::cout<<obj.id<<std::endl;

 if(id==obj.id){
  return true;
 }else{
  return false;
 }
}


static variables must be doing something a bit weird???
Ah... so... you need each object to be initialized with a unique value. The problem isn't with the operator, then... you could use some sort of counter passed to your object upon initialization.

static doesn't do what you think it does...

-Albatross
Why don't you use the object's address, this, as the unique id you want?

EDIT: Also pass the argument by reference in your == operator, like this: bool Team::operator ==(Team & obj). You see, if you pass it by value, a new object is created which has a different id from the object you pass. That's what caused you the problem. Other than that, the solution with a static int uniqueID and an int id for each object is fine. But it's simpler to use the objects' addresses as the ids.
Last edited on
I tried to pass by reference like you said but it still gave the value 2 for each objects uniqueID. Surely an overloaded operator function doesn't create a new object even if pass by reference was used, does it?? That is the behaviour it is displaying anyway.

although I don't quiet understand what you mean by
Why don't you use the object's address, this, as the unique id you want?


this is what I changed it to :

1
2
3
4
5
6
7
8
9
10
11
bool Team::operator ==(Team &obj){
 std::cout<<this->uniqueID<<std::endl;
 std::cout<<obj.uniqueID<<std::endl;

 if(this->uniqueID==obj.uniqueID){
  return true;
 }else{
  return false;
 }
}


Two things:

1) nobody in this thread overloaded the == operator properly. Const correctness, people.
2) You're misunderstanding static members


For #1, the == operator should look like this:

1
2
3
4
bool Team::operator == (const Team& obj) const  // note the 2 'const's here
{
  //...
}


For #2, static members are shared by all objects. They're basically global variables, just with limited scope. Because 'uniqueID' is static, there is one uniqueID for all instances of Team. Each team does not have it's own 'uniqueID'.

If you change a static member var in one class, you change it for all classes. They all share the same variable. That's the whole point of the static keyword.

if(this->uniqueID==obj.uniqueID){ <- this will always be true because you're comparing the the same variable to itself. it's like doing this:

1
2
int foo = 5;
if(foo == foo){ // will always be true 


hmm a simple work around was assigning an int called id, to the static uniqueID in the constructor and then comparing the int id instead of the static uniqueID.


This isn't really a "workaround", this is what you need to do. If you want each team to have it's own ID, you need a non-static member.
Last edited on
Try this:

1
2
3
4
5
6
7
8
9
10
bool Team::operator ==(const Team &obj) const{
 std::cout<<this<<std::endl;
 std::cout<<&obj<<std::endl;

 if(this==&obj){
  return true;
 }else{
  return false;
 }
}

EDIT: Added const keyword.
Last edited on
@Disch - brilliant thanks, I finally understand static! I thought they were unique to each object. They are more like a class variable

@m4ster r0shi - thanks alot, for that snippet, I never would of thought of comparing them like that


thanks to everybody who replied...right back to the book :D
Topic archived. No new replies allowed.