Overloading: What is it good for!?

This is a discussion on Overloading: What is it good for!? within the C++ Programming forums, part of the General Programming Boards category; I have yet to use overloaded functions or constructors in any of my applications. That includes 2D games and the ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Overloading: What is it good for!?

    I have yet to use overloaded functions or constructors in any of my applications. That includes 2D games and the like. A lot of people say it's the best thing ever, but I don't see what could possibly be done with this. I've been doing C++ for over a year, and haven't used this even once, even though I understand the concept. Can someone show me an example of how this could be used effectively and not just for preference reasons? Thanks!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've been doing C++ for over a year, and haven't used this even once
    Then you haven't needed it yet. There's nothing wrong with that at all.

    >Can someone show me an example of how this could be used effectively and
    >not just for preference reasons?
    An excellent example of overloading would be the << and >> operators that you probably use heavily with iostreams.
    My best code is written with the delete key.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Well, that would be operator overloading correct? I was specifying an example to be based on function overloading or even constructor overloading. Yes, I have used those operators alot!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was specifying an example to be based on function overloading
    An overloaded operator is a function as well.

    >or even constructor overloading
    Constructors are even easier. Take a look at the string class. For member function overloading, take another look at the string class.
    My best code is written with the delete key.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    If you've ever had a constructor that takes an argument, then you probably could have overloaded it. More often than not, my classes will have a default constructor because the argument it takes is optional when it's instantiated.

    As for overloading operators... they're really just a convenience thing. You could take any overloaded operator and simply make a function that does the same thing, but there are lots of times where specific operators simply seem like a logical replacement for a function.

    I might also dare to say that templating could be considered overloading since that's essentially what the compiler does when you compile the code.
    Last edited by SlyMaelstrom; 08-25-2006 at 12:42 PM.
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Aug 2006
    Location
    Slovenia
    Posts
    12
    If you are working with CString you can't use << >> unless you overload them:
    Code:
    // Operator << for input of the object
    ostream& operator<<(ostream& os, const CString& str)
    {
    	os << (const char*) str;
    	return os;
    }
    
    // Operator >> for output of the object
    istream& operator>>(istream& is, CString& str)
    {
    	char s[80];
    	is >> s;
    	str = s;
    	return is;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If you are working with CString you can't use << >> unless you overload them
    That's a shame. Good thing std::string is vastly superior. By the way, your overloaded operator>> is exceptionally poor. First, you impose a hidden size limitation by using an array. Second, you don't enforce that limitation when reading into the array. The former is poor programming practice and the latter is potential undefined behavior.
    My best code is written with the delete key.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    I know operator overloading but I rarely use it. As for function overloading, it can be useful when you want to use two functions that have the same name

    Code:
    ijnt func ( int x, int y )
    {
    // code
    }
    
    // overloaded function
    float func ( float x, float y )
    {
    }

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,511
    Quote Originally Posted by Sentral
    Well, that would be operator overloading correct? I was specifying an example to be based on function overloading or even constructor overloading. Yes, I have used those operators alot!
    Well, good examples of function overloading occur on the standard library. Take a look at the std::string.find() for instance, http://www.cppreference.com/cppstring/find.html, or the STL containers for constructor overloading examples like the std::map object for instance, http://www.cppreference.com/cppmap/m...structors.html
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Sentral
    I have yet to use overloaded functions or constructors in any of my applications.
    You mean you haven't ever concatenated two std::strings with + ? I have a hard time believing that.

    If you haven't overloaded any operators yourself, that's fine (although it suggests you're not using the full power of the STL), but pretty much everyone uses operators that are overloaded by some library. (Not using them probably means that you're not using a proper string class, no smart pointers, no index access operator for accessing your smart array, ... in other words, you're missing out on a lot of good programming practice.)

    Overloading operators is something you typically do in general-purpose libraries, not in applications.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> If you are working with CString you can't use << >> unless you overload them
    operator << should definitely work, since CString has an implicit conversion to const char*. Although I would imagine that its not common to use fstreams and the MFC string class in the same place.

  12. #12
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You mean you haven't ever concatenated two std::strings with + ? I have a hard time believing that.
    I didn't realize that it would be considered overloading! Maybe I have used it!

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Sentral
    I didn't realize that it would be considered overloading! Maybe I have used it!
    you probably used it in your first ever C++ program..
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "Hello, World!";
    }
    Although at the time, its unlikely you were told that << is an overloaded bitwise-left-shift operator

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  2. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 04:38 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 03:23 PM
  4. Replies: 8
    Last Post: 11-27-2004, 02:12 AM
  5. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM

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