Like Tree1Likes
  • 1 Post By Elysia

What is the main purpose of operator overloading

This is a discussion on What is the main purpose of operator overloading within the C++ Programming forums, part of the General Programming Boards category; Hi i am currently learning c++ and just went through operator overloading.My question is in what situations would this be ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    What is the main purpose of operator overloading

    Hi i am currently learning c++ and just went through operator overloading.My question is in what situations would this be used for

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    To provide more natural syntax.
    Say you have a class Int. It is then more natural (and easier to read) to write i1 = i2 + i3, rather than i1.set(i2.add(i3)).
    Epy likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Quote Originally Posted by sigur47 View Post
    Hi i am currently learning c++ and just went through operator overloading.My question is in what situations would this be used for
    Truth be told, not every class needs overloaded operators. Most operations are expected to stand for themselves (2 + x) or mirror something the language made you learn (as in *ptr). Probably, the most important operator you could write is assignment. Overloaded operators become more convenient when you start using the STL to do things: certain algorithms require operator<, or operator().

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Suppose you wanted to implement matrices, so you define yourself a new class `Matrix`, probably as a template. Matrices have well defined multiplication, addition and transpose operations. You could implement these by overloading `operator*`, `operator+` and either `operator!` or `operator~` instead of implementing them as member functions `add()`, `multiply()` and `transpose()`. That would make the code operating on `Matrix` type variables look and read more natural. Which is one of the benefits of operator overloading.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. = operator overloading
    By linuxdude in forum C++ Programming
    Replies: 21
    Last Post: 09-21-2007, 08:48 AM
  2. Purpose of Operator Overloading
    By xmltorrent in forum C++ Programming
    Replies: 11
    Last Post: 08-09-2006, 06:23 PM
  3. Purpose of Overloading Operators
    By luckygold6 in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2003, 08:14 PM
  4. Overloading * operator
    By LouB in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2002, 04:35 PM
  5. operator overloading
    By ihsir in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2002, 03:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21