Sign in to follow this  
Followers 0
schoel

Defining your own operators

9 posts in this topic

Is it possible to define your own operators?

For example, if I would like to define ~= for checking if two numbers are almost equal.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

this is possible in C++... just for the fact that operators are in the end functions, and nothing more than that

so use a function instead :)

Func _AlmostEqual($num1, $num2)

some code

return YourResult (True or False)

EndFunc

Cheers :idea:

oooh I'm so glad I'm learning c++ :party:

Edited by torels

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

Yeah, I'm aware that it is possible in C++.

I know that they are just functions but the code would be a lot more readable if I could make it an operator. I will have lots and lots of comparisons and an operator would just make it a lot nicer.

Share this post


Link to post
Share on other sites

well

when you are working with things the level of ~= something like _AlmosEqual(5,5.2) in its palce doesn't ruin your code so much you know :party:

nor does any other function if sufficently descriptive and clearly named :)


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

An operator wouldn't even be enough to satisfy an "almost equal" comparison, you would need to also specify a tolerance AND whether its absolute or relative.

Share this post


Link to post
Share on other sites

An operator wouldn't even be enough to satisfy an "almost equal" comparison, you would need to also specify a tolerance AND whether its absolute or relative.

surely

but with Operator Overloading (as schoel said) it would be possible :)


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

surely

but with Operator Overloading (as schoel said) it would be possible :)

Only if the tolerance is static and not user specified.

Operators only allow 1 operand on each side so how would I go about specifying a tolerance? You can't. Even in C++ this would be useless.

Share this post


Link to post
Share on other sites

Yeah I was thinking about a static tolerance. I guess I will have to use a function though.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You could do something like

bool operator ~=(double, vectordouble) // not a real operator

vectordouble operator ,(double, double)

and then just

if (myvar ~= (1.3, 1.0))

And voila.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0