Hi! I am having some problems with using the operator= on an dereferences object. I have the code...

Julian j1(2319,1,7);
Date * dp = new Gregorian(2319,1,23);	
cout << (*dp) << " " << typeid(*dp).name() << endl;	
*dp = j1;	
cout << (*dp) << " " << typeid(*dp).name() << endl;	
j1 = *dp;	
cout << (j1) << " " << typeid(j1).name() << endl;

which gives me the output

2319-01-23 N4lab29GregorianE
2319-01-07 N4lab29GregorianE
operator= Julian
2318-12-22 N4lab26JulianE

The problem is that *dp = j1 does not call the operator= in Gregorian. Instead it seems like a synthesized function is being called.

Here is the operator= definition for Gregorian.

Gregorian & Gregorian::operator= ( Date const & d ) {		
	cout << "operator= Gregorian" << endl;
	Gregorian temp(mod_year, mod_month, mod_day);
	temp += d.mod_julian_day();
	current_year = temp.year();
	current_month = temp.month();
	current_day = temp.day();
	return *this;
}

This is not being called only when I dereference the object and use the assignment operator.

To clarify, Gregorian and Julian inherits from Date which is abstract.

Can it have something to do with that dp is a Date pointer and when assigning to dp the synthesized assignment operator of Date is used instead of Gregorian?

Edited 6 Years Ago by Nick Evan: Added code-tags

Oh i solved it!

I just added operator= as a pure virtual function in Date and that did the trick!

Let me know if anyone has a better solution.

This article has been dead for over six months. Start a new discussion instead.