I'm trying to overload == to work with two pointers. The declaration is actually expecting a & and a * and I'm not sure how to do it. Any help appreciated. The class was cut down to save space and these are in their files, but everything else works.
CODE
#ifndef __clock_type_h
#define __clock_type_h
#include <iostream>
#include <string>
using namespace std;
class clockType
{
public :
friend ostream& operator<<(ostream& the_stream, clockType &the_clock);
friend ostream& operator<<(ostream& the_stream, clockType *the_clock);
bool operator==(clockType &the_clock);
bool operator==(clockType *the_clock);
void setTime(int new_h, int new_m, int new_s);
private:
int hours;
int mins;
int secs;
}; // endof class clockType
#endif __clock_type_h
// this works
void clockType::setTime(int new_h, int new_m, int new_s)
{
hours = new_h;
mins = new_m;
secs = new_s;
roll_time();
}
// this works
ostream& operator<<(ostream& the_stream, clockType &the_clock)
{
cout << "\nclockType operator << & actually executed\n"; // zebra
the_stream << the_clock.hours << ":" << the_clock.mins << ":" << the_clock.secs;
return(the_stream);
}
//this works
ostream& operator<<(ostream& the_stream, clockType *the_clock)
{
cout << "\nclockType operator << * actually executed\n"; // zebra
the_stream << the_clock->hours << ":" << the_clock->mins << ":" << the_clock->secs;
return(the_stream);
}
// this works
bool clockType::operator==(clockType &the_clock)
{
cout << "\nclockType operator == & actually executed\n"; // zebra
if(
(hours == the_clock.hours) &&
(mins==the_clock.mins) &&
(secs==the_clock.secs)
) { return(true); }
else { return(false); }
}
// this never executes
bool clockType::operator==(clockType *the_clock)
{
cout << "\nclockType operator == * actually executed\n"; // zebra
return(true); //zebra
if(
(hours == the_clock->hours) &&
(mins==the_clock->mins) &&
(secs==the_clock->secs)
) { return(true); }
else { return(false); }
}
void main()
{
clockType *a_clock = new clockType(); // get the pointers
clockType *b_clock = new clockType();
clockType c_clock;
c_clock.setTime(11,21,75); // and the objects
clockType d_clock;
d_clock.setTime(11,21,75);
cout << "\nc_clock is "; // test == on the objects
if(c_clock == d_clock)
{
cout << "EQUAL";
}
else { cout << "NOT Equal"; }
cout << " to d_clock\n";
a_clock->setTime(11, 21, 75);
b_clock->setTime(11, 22, 15);
cout << a_clock << " " << b_clock; // here is where == does not execute on the pointeres
cout << "\na_clock is ";
if(a_clock == b_clock)
{
cout << "EQUAL";
}
else { cout << "NOT Equal"; }
cout << " to b_clock\n";
delete a_clock;
delete b_clock;
cout << "\n\n\n\n"; // Move IDE Msg down
system("pause");
}
This post has been edited by jeronimo0d0a: 5 Mar, 2008 - 11:57 AM