I'm studying this rudimentary string class and I have a few questions about the
Code:
operator[]
overloading it uses to access a character at a certain position. Here it is:


Code:

class String {
    protected:
        char * itsString;
        unsigned short itsLength;
    public:
        // some constructors...
        // some accessors...
        char operator[](unsigned short offset) const;
        char & operator[](unsigned short offset);
        // more methods...
};


The first overload is for read-from instances while the second is for assign-to instances. First of all, how is the function overloading determined: by the return value (which I didn't know was possible) or by whether or not the member function is const? Does the first function have to be a const member? Sorry, but const has seriously been confusing me lately and I feel it's doing more harm than good by making me trace back to see what can and cannot be changed.

Another thing; assuming the overloading is done by return value, do all assign-to situations require a reference and all read-from situations require a value by convention? If so, where can I find the specifications on what kind of function certain operators in certain situations expect for overloading? Hope that made sense. Thanks.
Have you taken a look at some documentation yet? And as far as the specifications, take a look at the official C++ spec for some (hopefully) helpful information. Smile I'll try to be more helpful after you take a look.
I have it now. Apparently you can't overload by return type since it creates some ambiguity with type casting. Turns out you can overload by whether or not the functions is const.
Progbeard wrote:
I have it now. Apparently you can't overload by return type since it creates some ambiguity with type casting. Turns out you can overload by whether or not the functions is const.
Excellent, glad you figured that out. Smile So are all your overloading questions answered, or do you still have confusions about some issues?
howjukia wrote:
It is very helpful for me.
Thanks for it.

botty mcbotsauce?
Yup, it added a signature later on. Deleted but still needs suspension/deactivation.
Nice catch, Cemetech staff. Thanks for that.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement