Jump to content

new operator

- - - - -

  • Please log in to reply
5 replies to this topic

#1
jaiii

jaiii

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Hi,
how can I write new operator

for example:

bool operator G (int i,j){return i>j};

thankx

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
you can't create new operators, you can only redifine existing ones, and then only with atleast one user-defined type as the parameters.

#3
jaiii

jaiii

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Category

Operators, C++-Specific Keywords

Syntax

operator <operator symbol>( <parameters> )

{
<statements>;

}

<operator symbol> Don't be 'G' ?

This from help BCB 6.0

#4
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
again, you can't create new operators. You can only overload the ones you already have (and not all of them). If you could create new operators, then the compiler would need to have a very dynamic operator table, which C++ doesn't have (you define any operator you like in Prolog, and probably some other languages.)

See Operators in C and C++ - Wikipedia, the free encyclopedia

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You can create class methods to do what you want. For example:
class A {
 public:
  size_t value;

  bool G (const A& rhs) const {
    return value > rhs.value;
  }
};

Instead of doing something like this:
lhs G rhs;

You can do this:
lhs.G(rhs);

Edited by dargueta, 29 June 2010 - 09:18 PM.
Fixed syntax violation

sudo rm -rf /

#6
jaiii

jaiii

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Thank I try it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users