Overloading based on return value/const?

This is a discussion on Overloading based on return value/const? within the C++ Programming forums, part of the General Programming Boards category; Hi there - I'm mainly a C+Java+Python+Lisp programmer, and though I've done a fair bit of C++ in the past ...

  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
    21,732
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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,304
    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, 04: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

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