Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,487 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,699 people online right now. Registration is fast and FREE... Join Now!




overloading == with pointers

 
Reply to this topicStart new topic

overloading == with pointers

jeronimo0d0a
5 Mar, 2008 - 11:55 AM
Post #1

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
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
User is offlineProfile CardPM
+Quote Post

schnalf
RE: Overloading == With Pointers
5 Mar, 2008 - 12:21 PM
Post #2

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 124



Thanked: 2 times
My Contributions
when you remove the == operator for the pointers, you will see that the program is still doing the same like before. i think it calls a standard == operator to compare the adresses of your objects and not your operator.
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Overloading == With Pointers
5 Mar, 2008 - 12:46 PM
Post #3

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
It works with an object and a pointer. I think it is just declared wrong.

if( *a_clock == b_clock) works

This post has been edited by jeronimo0d0a: 5 Mar, 2008 - 01:05 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month