Saturday, October 22, 2005

Operator overloading problem


I've overloaded the operator <. Since it's a binary operator, I
implemented it as a member function and just pass it another object,
the code looks like this:

class StudClass{
public:
StudClass();
StudClass(bool fcfs, int initTime, int initRi)
{
arrivalTime=initTime;
requestedTime=initRi;
if(fcfs==true)
priority=&arrivalTime;
else{ priority=&requestedTime; }
};

bool operator<(const StudClass& student) const
----> { return(*this->priority < *student.priority); }
int *priority;
bool fcfs;
private:
int arrivalTime;
int requestedTime;
};

When the operator< is called the two different pointers this->priority
and student.priority point to the same space, an integer stored in the
passed object (student). When I changed priority to an integer it
worked correctly. Is there something wrong with using pointers from
two different objects?

Here's a copy of the debug information:

*student.priority 10 __int32
- priority 0x0012f01c __int32*
*StudClass.priority 10 __int32
- this 0x02cb5fb0 { priority=0x0012f01c fcfs=false arrivalTime=0
...} StudClass*
arrivalTime 0 __int32
fcfs false bool
- priority 0x0012f01c __int32*
*(*this).priority 10 __int32
requestedTime 30 __int32

--Drew

0 Comments:

Post a Comment

<< Home