3 Replies - 974 Views - Last Post: 19 March 2008 - 02:42 PM Rate Topic: -----

#1 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

pre and post ++ ovelooaded operator

Posted 19 March 2008 - 12:51 PM

I'm having trouble with the operator++ in my class. to further complicate things, cout is doing different things if I call.

cout << "\n\tc_clock " << c_clock << " as ++c_clock " ;
cout << ++c_clock ;
vs.
cout << "\n\tc_clock " << c_clock << " as c_clock-- " << c_clock-- ;

I know this should be simple, but I'm not getting it. Any help appreciated.

clockType& operator++(clockType &the_clock) 
{
	cout << "\n++PREfix operator executed\n";

	++the_clock.secs;
	the_clock.roll_time();
	return(the_clock);
}

clockType& operator++(clockType &the_clock, int) 
{
		cout << "\nPOSTfix++ operator executed\n";

	clockType temp;
	temp = the_clock;
	the_clock.secs++;
	the_clock.roll_time();
	return(temp);
}



This post has been edited by jeronimo0d0a: 19 March 2008 - 01:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: pre and post ++ ovelooaded operator

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: pre and post ++ ovelooaded operator

Posted 19 March 2008 - 01:44 PM

I think you forgot to use the scope operator for the function definition.

clockType& clockType::operator++(clockType &the_clock)

clockType& clockType::operator++(clockType &the_clock, int)



cout << "\n\tc_clock " << c_clock << " as c_clock-- " << c_clock-- ;

You will need to define the decrement operator for the class clockType for the above statement to function as expected.


edit*: there are many more errors than mentioned above.

This post has been edited by letthecolorsrumble: 19 March 2008 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: pre and post ++ ovelooaded operator

Posted 19 March 2008 - 01:45 PM

This quwstion was answered by KYA @

http://www.dreaminco...wtopic33888.htm

some time back.
Thanks

I wasn't using the scope operator because it was a friend function. And I stuck the cout in it so I knew it was running, I just had a few slight errors in its definition. Once I got the ++, the -- is basically almost a carbon copy.

Thanks again.

This post has been edited by jeronimo0d0a: 19 March 2008 - 01:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: pre and post ++ ovelooaded operator

Posted 19 March 2008 - 02:42 PM

Well remember that these pre and post operators are a bit different in their structure from overloading normal operators. First they return versions of themselves and second they don't typically take a parameter except for when distinguishing between the pre and post operators.

Below is an example of how this will work...

#include <iostream>
using namespace std;


class example {
	int examplevalue;

public:
	// Constructor
	example(int val) { examplevalue = val; }

	// Pre increment (notice there is no dummy parameter)
	example operator++() {
		++examplevalue;
		return *this;
	}

	// Dummy value supplied to tell different between pre and post ++ 
	// This is post
	example operator++(int notused) {
		examplevalue++;
		return *this;
	}

	int showValue() {
		return examplevalue;
	}
};


 
int main() 
{ 
  // Create our class with initial value of 5
  example test(5);

  // Show it to check it is 5
  cout << "Example before increment: " << test.showValue() << endl;

  // post increment so it calls operator++(int notused)
  test++;

  // Show it after increment
  cout << "Example after increment: " << test.showValue() << endl;

  return 0; 
}



Read through the in line code and you will see how it works. Look at the design of the pre and post increment definitions in the example class and you should know where you are going wrong.

Enjoy!

"At DIC we be ninjas++ while others are --ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1