When i was reading A Tour of C++ (Author: Bjarne Stroustrup), in 49th page i came across a code like this.
class Vector {
private:
double∗ elem;
int sz;
public:
Vector(int s);
˜Vector() { delete[] elem; }
Vector(const Vector& a);
Vector& operator=(const Vector& a);
double& operator[](int i);
const double& operator[](int i) const;
int size() const;
};
These two lines came a bit weird to me when i first saw them. Why using const to overload operator[] ?
double& operator[](int i);
const double& operator[](int i) const;
Ofcourse somebody asked this question before.
I have a topic I'm confused on that I need some elaborating on. It's operator overloading with a const version and a non-const version.
// non-const
double &operator[](int idx) {
if (idx < length && idx >= 0) {
return data[idx]
}
throw BoundsError();
}
I understand that this lambda…
One of the answer is
When both versions are available, the logic is pretty straightforward:
const
version is called forconst
objects, non-const
version is called for non-const
objects. That's all.
The point of having two versions is to implement "read/write" access for non-const
objects and only "read" access forconst
objects. Forconst
objectsconst
version ofoperator []
is called, which returns aconst double &
reference. You can read data through that const reference, but your can't write through it.
in the comments of the question somebody add this video
In the second part of the video Cherno show the case how to use const in getter function. It really explains well the answer above.
But in the first part of the video there is a part which i don't understand quite well about using const keyword in pointers.
const int* a = new int;
int* const a = new int;
int const* a = new int;
const int* const a = new int;
Somebody ofcourse asked it before too and i found a beautiful answer.
The trick is to read the declaration backwards (right-to-left):
const int a = 1; // read as "a is an integer which is constant"
int const a = 1; // read as "a is a constant integer"
Both are the same thing. Therefore:
a = 2; // Can't do because
…
after reading that trick in the answer it becomes more easy to understand.
Lets check those 4 lines one by one using the trick.
const int* val = new int;
val is a pointer, to integer which is constant.
*val -> dereference of val(integer) can not be change.
val -> memory address of val can be change.
int* const val = new int;
val is a constant pointer to integer.
*val -> dereference of val(integer) can be change.
val -> memory address of val can not be change.
int const* val = new int;
val is a pointer to a constant integer.
*val -> dereference of val(integer) can not be change.
val -> memory address of val can be change.
const int* const val = new int;
val is a constant pointer to an integer which is constant.
*val -> dereference of val(integer) can not be change.
val -> memory address of val can not be change.
Here is a song for you
https://soundcloud.com/nocontent/dj-planet-express-ft-augustix-angel
Have a good day!
Oldest comments (0)