Doubt in video -> Container design

Below is the code from the video…

Q1. what does ‘const’ do ? whats the difference in line 13 vs 25 / 29 ? are there more situations to use const ?

Q2. Please explain line 53 of the code ?

hello @prerak_semwal

const is keyword that we use to declare something as constant(if using const with object or variable then its value cannot be changed ,
if using const with any function then that function cannot make any changes in passed variables ). wherever u feels the need of constant object or function then u can use it.

we are overloading [ ] operator .
so whenever we will try vectorname[i] then in that case that function (in lin 53) will be called and it will return value present at index i

@aman212yadav

but in case of size() there are no parameters why we used const then ?

is operator in-buit ( keyword ) ?

It is recommended practice to make as many functions const as possible so that we can avoid any accidental change.

yes operator is a keyword in c++.

@aman212yadav
then is it always used as -> operator[]

or
is it sometime used as only -> operator ?

Is there some other situation where operator is used (a small example) ?

pls read about operator overloading . its a complete oops topic.

@aman212yadav

ok
oops is a future topic in my course… but just yes/no question is it always used as -> operator[]

no,there are other operators as well like +,- etc

@aman212yadav

so here [] is just like +, - , % , ^ …etc , right?

yes…

1 Like