The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

corwin17's c++ corner: 03/27 operator overloading by corwin172014-03-27 10:45:15
  operator overloading by lenmear 2014-03-27 11:05:43
First, your understanding of operator overloading and the number of arguments isn't quite right. When you overload an operator, you need to use the same number of arguments as in the built in operator. Here are a couple of examples:
operator+ is a binary operator, you have to supply two arguments.
operator- is a binary operator, you have to supply two arguments.
operator- is also a unary operator (int a = 1; -a;), you have to supply one argument.

Second, most operators can be overloaded in two ways: as a class method, or a stand-alone function:
class matrix
{
  public:
    // yadda yadda yadda
    matrix operator+(const matrix& n) const;
};

matrix operator+(const matrix& m, const matrix& n);
In the former case, the first operand is implied to be *this, so you only have one formal argument. In the latter case, the operator is not part of a class, so you must explicitly declare both operands as arguments.

Finally, the statement you want to type, C = A + B;, is actually two operators: = and +. operator=() can be overloaded just like other operators; it's unary so:
class matrix
{
  public:
    // yadda yadda yadda
    matrix& operator=(const matrix& source)
    {
      // implement copying from source to *this
    }
}
[ Reply ]
    I think I understand, but by corwin172014-03-27 11:49:10
      Use the accessors by lenmear2014-03-27 12:00:32
        Danger, Will Robinson! by lenmear2014-03-27 12:27:00
          And people wonder why I refuse to learn OOP. by firehawk2014-03-27 12:38:20
            And why I've refused until thrust upon me! (n/t) by corwin172014-03-27 12:42:35
            OOP isn't that bad, it's the unmanaged langauges by Freakazoid2014-03-27 12:47:50
              I use PHP, and I've seen PHP OOP... by firehawk2014-03-27 12:53:53
          <Deleted><Deleted>2014-03-27 22:45:15
            Whoa! Mods! B-Usted Horizontal Scroll! by wwill2014-03-27 22:25:19
        Thought of that just after submitting, but... by corwin172014-03-27 12:40:52

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)