Operator overload?

Overload an operator of a base type? [double]

Page 1 of 1

4 Replies - 567 Views - Last Post: 06 February 2010 - 09:41 AM Rate Topic: -----

#1 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Operator overload?

Posted 06 February 2010 - 08:39 AM

Is there anyway to overload an operator of a base type like double or float?

Something to this effect, but the compiler tells me the operator must be of a class type... :(
double operator^( double operand )
{
	return pow((*this), operand);
}

Is This A Good Question/Topic? 0
  • +

Replies To: Operator overload?

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Operator overload?

Posted 06 February 2010 - 08:45 AM

You can only overload operators of a class, and operators of basic types to be compatible with your classes. Standard C++ however doesn't allow someone to change the way basic type operators work :)

Hope I helped :D
Was This Post Helpful? 0
  • +
  • -

#3 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Re: Operator overload?

Posted 06 February 2010 - 09:24 AM

would it be possible to make a struct or something that derives from these types? hmmmm.... I think this can work... I'll post some code in about ten minutes... maybe less......
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: Operator overload?

Posted 06 February 2010 - 09:36 AM

The question is why? You're dealing with primitives, use pow()?


edit: Specifically, why the wrapper? Could you provide a broader context? It would allow for pursuit of other options perhaps.

This post has been edited by KYA: 06 February 2010 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Re: Operator overload?

Posted 06 February 2010 - 09:41 AM

The ultimate goal is to make a string parser with a declaration something like
double Parse(string function, char variable, double value);


--- maybe an extension for the string class?

so the average user probably would not know the pow() function, and would feel much more at home using the caret operator.

edit:
what happened to the inline tags??

This post has been edited by Vermiculus: 06 February 2010 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1