Overload = operator on custom class

Hi all, I've looked through £100's worth of text books and all the code I can find but I jsut can't work out how to be able to make a comparison between two class variables, i.e.

if ( a_class A = a_class B ) { return 1; }

I'm trying to apply this concept to compare dates in a custom date of birth class:


Here's the header file:

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
#ifndef DOB_H
#define DOB_H

#include <iostream>

// Date of birth class
class dob {

	protected: 
		
		int _d, _m, _y;
	
	public:
		
		dob(); // Default constructor
		dob(int d, int m, int y); // General constructor
	
		int d() const {return _d;} // Return day of birth
		int m() const {return _m;} // Return month of birth
		int y() const {return _y;} // Return year of birth

                // CANT GET THIS BIT TO WORK
		//dob operator=(const dob &x); // operates on x

		~dob(); // Destructor

};

	// Output date of birth for intuitive display to user
	std::ostream& operator<< (std::ostream& os, const dob& t);

#endif
	


And here's the function file:

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
// Function file for the date of birth class... __ ...inherited by the ftree class.

#include "dob.h"
using namespace std;

// Default constructor - set date, month and year to zero
dob::dob(): _d(0), _m(0), _y(0) {}

// General constructor
dob::dob(int d, int m, int y): _d(d), _m(m), _y(y) {}

// Assignment operator
//dob::dob operator=(const dob &x); // operates on x
// _d(d) = _d(x);
// _m(m) = _m(x);
// _y(y) = _y(x);


// Display date of birth in dd/mm/yy format for output
std::ostream& operator<< (std::ostream& os, const dob& t) {
  os << t.d() << '/' << t.m() << '/' << t.y();
  return os;
}

// Define destructor
dob::~dob() {}


It seems like such a simple concept in my head but I cant get the code to reflect that!

Sorry for the trouble, any help would be greatly appreciated!

Thank you very much,
Gareth.
The assignment operator is a tad unusual as far as operator overloading goes.
It generally takes the form:
1
2
3
4
5
6
7
class A {
  public:
    A& operator = (const A& a) {
      // do assignment stuff here
      return *this;
      }
  };

There are other caveats.

1. Watch out for self assignment.
http://www.parashift.com/c++-faq-lite/assignment-operators.html

2. If you overload the assignment operator, you should almost always overload the copy constructor also (and vice-versa).

3. Beware the nasties:
http://www.icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html

Good luck.
Thanks for your reply! Okay... I've spent ages messing about with this but I jsut can't get it to work, there must be 20 errors or so when I run my = operator. I've read both those sites but their examples are quite different from mine and I'm unable to translate them to my code.

Im at this stage so far:

1
2
3
4
5
6
7
8
9
10
11
dob& dob::dob operator=(dob &x) {
	
	_d = *x._d;
        
	_m = *x._m;
		
	_y = *x._y;

    return *this;

}


But it doesn't like that at all!

EDIT: Just thought... I'm trying to COMPARE two dates, not make one equal to the other, so whe I need to overload is the '==' operator.

Doh! Back to the drawing board.

Double Edit: void dob::dob operator==(dob &x) {} ???
Last edited on
Do you want assignment (operator=) or comparison (operator==)?

In your above code, you just need to remove the asterisks from *x.
However, you should also make your x parameter a const reference
and you also have an extraneous "dob":

 
dob& dob::operator=( dob const& x ) { ... }


If you want comparison, operator== is very similar in syntax:

1
2
3
bool dob::dob operator==( dob const& rhs ) {
   return _d == rhs._d && ...
}

Its a comparison operator I need. I tried this:

Base class
dob operator==( const dob &rhs ); // operates on x

Function file
1
2
3
4
// Comparison operator
bool dob::dob operator==( dob const& rhs ) {
   return _d == rhs._d && _m == rhs._m && _y == rhs._y;
}


but it comes up with all sorts of errors. Hmm.
That's because your syntax stinks... watch your syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// fooey.h

#ifndef FOOEY_H
#define FOOEY_H

class Fooey {
  private:
    int f_day, f_month, f_year;

  public:

    // Make == a friend function
    friend bool operator == (const dob& lhs, const dob& rhs);
  };

#endif 

1
2
3
4
5
6
7
8
9
// fooey.cpp

#include "fooey.h"

bool operator == (const dob& lhs, const dob& rhs) {
  return (lhs.f_day   == rhs.f_day)
     and (lhs.f_month == rhs.f_month)
     and (lhs.f_year  == rhs.f_year);
  }


Hope this helps.
Last edited on
It sure does! Tried and tested.

Thank you so much!
Topic archived. No new replies allowed.