Thread: Overloading based on return value/const?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    200

    Overloading based on return value/const?

    Hi there - I'm mainly a C+Java+Python+Lisp programmer, and though I've done a fair bit of C++ in the past I definitely don't consider myself an expert, so was wondering if someone with more detailed knowledge could help me out here.

    I'm using Qt for a project, and found myself wanting to use operator[] on a QMap. I tootle along to the QMap online documentation to see if there's anything I need to be aware of and find the following two function signatures for operator[] (here):

    Code:
    T &     QMap::operator[] ( const Key & key )
    const T QMap::operator[] ( const Key & key ) const
    One returns a modifiable reference to the T indexed by key, the other returns a const copy, and I'd quite like to know which one I'm getting!

    Now, I thought you could only overload methods based on their arguments - but clearly that's not what's distinguishing these two. I doubt it's based on return type, so I'm guessing it's somehow based on the const-"ness" of the object on which I'm using these methods? Am I close? What am I missing here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JohnGraham
    I'm guessing it's somehow based on the const-"ness" of the object on which I'm using these methods? Am I close?
    You guessed correctly
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Essentially, if you have a QMap object which is const, you get the const T version, and otherwise the T& version. So there you go. Now you know which one you will get.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Cheers for that - I'll think about `const' in a function declaration slightly different now. Useful to know.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you think of the 'this' pointer as just another (albeit hidden) argument, then you're really simply just overloading based on the arguments again.
    I.e. it's taking QMap *this or const QMap *this
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overly Strict Const Overloading
    By SevenThunders in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2010, 07:22 AM
  2. rewrite const property through overloading
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 02-26-2008, 05:42 AM
  3. Subscript operator overloading woes (const related?)
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2007, 10:23 PM
  4. function overloading. const T and const T* parameters
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-07-2006, 11:04 AM
  5. Operater overloading - (const char + CString)
    By auth in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2002, 09:22 AM