operator keyword

what are its uses???

Page 1 of 1

4 Replies - 18308 Views - Last Post: 07 December 2007 - 05:06 PM Rate Topic: -----

#1 ibaraku   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

operator keyword

Posted 07 December 2007 - 03:40 PM

What are the uses of the keyword "operator" in c++, I've runned on a couple of definitions but they are not so clear. Can anyone here just give a general idea.
Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: operator keyword

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: operator keyword

Posted 07 December 2007 - 03:47 PM

C++ allows you to overload operators... That is it allow you to create a custom version of operators such as the + operator. So if I had a class called MyVector and I wanted to define addition for MyVector so that I could use a bit of code like:

MyVector a, b, c;
...
c = a + b;

I can overload the addition operator (+).

Operator overloading
Was This Post Helpful? 0
  • +
  • -

#3 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: operator keyword

Posted 07 December 2007 - 03:58 PM

it also allows you to declare conversion operators, which allow safe, implicit casting between classes and other types

for example, if a class had an internal "good" or "bad" state, it might be desirable to be able to evaluate an object inside a conditional statement - This would be a good case for a bool conversion operator
class myclass
{
	bool state;
public:
	myclass() : state(true) {}
	operator bool()
	{
		return state;
	}
}; 

int main()
{
	myclass mc;
	if( mc )
	{
		// 'state' variable in mc is true
	}
} 

This post has been edited by Bench: 07 December 2007 - 03:59 PM

Was This Post Helpful? 1

#4 ibaraku   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Re: operator keyword

Posted 07 December 2007 - 04:06 PM

View PostNickDMax, on 7 Dec, 2007 - 03:47 PM, said:

C++ allows you to overload operators... That is it allow you to create a custom version of operators such as the + operator. So if I had a class called MyVector and I wanted to define addition for MyVector so that I could use a bit of code like:

MyVector a, b, c;
...
c = a + b;

I can overload the addition operator (+).

Operator overloading


Thanks for the answer and link
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: operator keyword

Posted 07 December 2007 - 05:06 PM

View PostBench, on 7 Dec, 2007 - 03:58 PM, said:

it also allows you to declare conversion operators, which allow safe, implicit casting between classes and other types


You know I had COMPLETELY forgotten about that! What is funny is that I saw such a thing not to long ago and sort of squinted at it but it was not until I read your post my memory was jogged and I remembered (if it were not for the example I probably would not have).

:) thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1