-1

I was reading this book called "Effective C++ Third Edition 55 Specific Ways to Improve", and while reading it I came across the topic of constness , i don't get the difference between the overloaded const operator[] which has bitwise and member function constness,

const char operator[](size_t t) 
    const{
         return text[t];
    }

, and the overloaded non-const [] which doesnt have to do anything with constants

char operator[](size_t t) 
    {
         return text[t];
    }

I have tried doing some operations with and without the const and non-const overloaded[] , but still haven't seen a difference , is this is used only to make the code readable ? or if there is any difference then what is it ?

This is my String class

class String
{
    int len = 0;
    char text[];
    public : 

    String( const char c[])   
    {
        strcpy(text , c); 
        len  = strlen(text);
    }
    int length();
    const char operator[](size_t t) 
    const{
         return text[t];
    }
     
    char operator[](size_t t) 
    {
         return text[t];
    }
};
Carlos
  • 5,991
  • 6
  • 43
  • 82
X caliber
  • 52
  • 4
  • 1
    `char text[];` is not a valid member. (That book is not a beginner's book; it was "written with the aim of being the best second book C++ programmers should read".) – molbdnilo Aug 17 '20 at 08:47
  • 1
    Show what operations you tried. If they weren't done on `const` instances, it doesn't matter a bit. But if you left out the `const` overload and tried calling `operator[]` on a `const String`, you would find it couldn't compile. Then, once you add that, it's logical that an accessor to a `const` object should/must return a `const` version of the contained data. This is basic beginner-level stuff & doesn't warrant a question. If you don't know what something is yet, then don't use it yet. But you'll probably find out soon enough what the many points of `const` are (essential and good practice). – underscore_d Aug 17 '20 at 08:47
  • 2
    The book doesn't actually contain a (useless) `const char operator[]`. It contains a `const char& operator[]`. If you don't understand why one `const` is useless and the other is not, or why `&` is there, you might need to read another C++ book. – n. m. could be an AI Aug 17 '20 at 08:54
  • in case you want a good book: [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/995714) – phuclv Aug 17 '20 at 11:03

1 Answers1

0

Const methods can be called on const objects, since they do not modify the object:

For example if you have class Foo:

class Foo {
  ...
  const methodA();
  methodB();
  ...
};

And use it like this:

Foo objectA;
const Foo objectB;

you can call both methods on objectA but only methodA() on objectB:

objectB.methodA();
objectB.methodB(); // <-- invalid

Same applies for operators.

Nikola
  • 125
  • 1
  • 8
  • To modify class internal variables. For example if this is the class: `class Foo { int someVar; public: void SetVar(int newVar) { someVar = newVar; }; }` SetVar can not be declared const! – Nikola Aug 17 '20 at 08:56