Thread: Overloaded operator[] question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Overloaded operator[] question

    I have a simple question but could find the answer in a thread...

    I have a myString class and overloaded the operator[] with
    Code:
    char& operator[](unsigned int index);
    const char& operator[](unsigned int index) const;
    (Actually it's the same as in the vector class: http://www.cppreference.com/cppvecto...operators.html)

    In my main Routine I have the following:
    Code:
    myString test("hello");
    test[0]='c';
    printf("%c\n", test[0]);
    My question is, why in both cases only the first overloaded function is called... Is there a way to separate assignment and return-value?

    Greetings, Dee

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    printf is C. My guess is that it takes a char, not a const char. Try writing your own function:
    Code:
    void function(const char c) {
        cout << c;  // or printf
    }
    Can you even do that?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Wait, I have it. Use a const object and see if the second operator[] is called.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Thanks for your answer... Yes, that was my problem. I misinterpreted the way opf the second function! It will work when I use a const object...

    Greetings, Dee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM