GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Apr-2009, 03:14
Ndimangwa Ndimangwa is offline
New Member
 
Join Date: Dec 2008
Location: BANGALORE - INDIA
Posts: 11
Ndimangwa has a little shameless behaviour in the past

Defining New C++ Operators


I C++ we can overload an operators, that is fine!!

My question is how do I define a new operator apart from overloading the ones existing?
Including how to set rules and principles of how the operator manipulates its operands?

Thank you much!!!!
  #2  
Old 08-Apr-2009, 05:41
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Defining New C++ Operators


Quote:
Originally Posted by Ndimangwa
I C++ we can overload an operators, that is fine!!

My question is how do I define a new operator apart from overloading the ones existing?
Including how to set rules and principles of how the operator manipulates its operands?

Thank you much!!!!


Consider the following:

CPP / C++ / C Code:
#ifndef Money_h_
#define Money_h_

#include <cstdlib>
#include <iostream>
#include <string>

class Money
{
public:
    Money();
    Money(Money const& rhs);
    Money& operator=(Money const& rhs);
    ~Money(); 

    Money(std::string const& amount);

#if _UNICODE
    Money(const wchar* amount);
#else
    Money(const char* amount);
#endif

    bool operator!=(Money const& rhs) const;
    bool operator==(Money const& rhs) const;
    bool operator<(Money const& rhs);
    bool operator>(Money const& rhs);
    bool operator<=(Money const& rhs);
    bool operator>=(Money const& rhs);

    Money& operator+(Money const& rhs);
    Money& operator-(Money const& rhs);
    Money& operator/(Money const& rhs);
    Money& operator*(Money const& rhs);

    Money& operator-=(Money& rhs);
    Money& operator+=(Money& rhs);

#if _UNICODE
    const wchar getCurrencySymbol() const;
    void setCurrencySymbol(const wchar symbol);
#else
    const char getCurrencySymbol() const;
    void setCurrencySymbol(const char symbol);
#endif
    friend std::ostream& operator<<(std::ostream& os, Money const& rhs);
    friend std::istream& operator>>(std::istream& is, Money& rhs);
protected:
private:
    int   m_lodp; // left of decimal point
    int   m_rodp; // right of decimal point
#if _UNICODE
    wchar m_currency_symbol;
#else
    char  m_currency_symbol;
#endif
};


#endif 


There are declarations for operators that would be inserted by the compiler if omitted and there are declarations for operators for which the compiler is unable to give any reasonable implementation.

The operator declarations are as follows:

assignment copy operator

inequality operator
equality operator
less-than comparison operator
greather-than comparison operator
less-than-or-equal-to comparison operator
greather-than-or-equal-to comparison operator

basic arithmetic operators

operators for assignment by addition and substraction

and finally, streaming operators for I/O.

Notably missing are ++ and -- operator declarations. At first glance, they appear to be relevant, but upon further investigation, their usage would be unclear.

Hopefully this will give you an idea of how to use operators more effectively. One should carefully examine the implementation such that expected behavior of defined operators is intuitive and easily anticipated. As in the case of not defining ++ and -- operators, since we do not easily know from which side of the decimal point they would work, any choice for them appears to be wrong in at least one or more expectations of their behaviors.

However, if we were declaring a class called Integer, we would then probably expect to see both ++ and -- declarations for postfix and prefix versions of these operators as the behavior of these would be easily anticipated by the user and therefore have an appropriate implementation.

See: operator-overloading for more info.



MxB
  #3  
Old 08-Apr-2009, 09:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Defining New C++ Operators


Quote:
Originally Posted by Ndimangwa
My question is how do I define a new operator apart from overloading the ones existing?
Overloading gives new meaning to the standard operators defined by the language.

In standard C or C++ you can not create new operators different from the ones defined in the language. Period. Full stop.

Regards,

Dave
  #4  
Old 21-Apr-2009, 05:13
siddhant3s siddhant3s is offline
New Member
 
Join Date: Apr 2009
Posts: 1
siddhant3s is on a distinguished road

Re: Defining New C++ Operators


Well, Bjarne Stroustup gives a good explaination why he didn't allowed "Home made operators" in C++. Here it is http://www.research.att.com/~bs/bs_faq2.html#overload-operator
Hope this reading will convince you
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using bitwise operators for masks, ifdefs, inttype.h, etc... Howard_L C Programming Language 3 20-Aug-2007 12:04
How to print an equation in order of precedence of operators shoegoe C Programming Language 7 02-Nov-2006 15:14
Class definitions for operators Darth Predator C++ Forum 1 12-Apr-2006 20:59
overloading operators in a class. fevershark C++ Forum 11 22-Feb-2006 20:34
Overloading of operators alcoholic C++ Forum 1 12-Dec-2005 05:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.