Week 6 Day 1 - C++ Trivia: Const

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=ORfs9Rtf9hY



Duration: 1:28:20
126 views
1


We started off with a code review of the Superhero Mart assignment. A part of the code review and then the learning point for today was on the use of const in C++. Const always means something along the lines of "Read Only", with several different ways it can appear in code:


Version 1 - Normal constant variables:
const int x = 42; //Makes an integer that is set to 42 and can't be changed.


Version 2 - Constant parameters (often done with call by reference parameters to keep them from changing accidentally):
void print_list(const List &list) { ... } //The variable list can't be changed by the function, despite being passed by reference


Version 3) Constant pointers (iterators work similarly)
int arr[100];

int y = 42;

const int *ptr = arr; //Common version
const int * const ptr2 = &y; //Less common version


A constant pointer allows you to read from a memory address but does not let you write to it. So "ptr[5] = 42" is bad code (you can't write to arr through the pointer) but "y = ptr[5];" works fine.


Note that "ptr++;" is perfectly valid code, which will point ptr at the second array element, which will still be read-only. If you don't want ptr to be able to change like that, use the less common version ("const int * const ptr = arr;"). If you do that, "ptr++;" will fail to compile as would "ptr[5] = 42;"



Version 4) Constant methods


void List::print_list() const { ... }


This only works with methods (i.e. member functions). By putting a const after the parameter list, this means that the method won't modify any of the member variables of the object we're within.


This is GOOD PRACTICE. Why? Because only constant methods can be called on constant variables, so mark every method constant if you can.



Version 5) Constant return values.


const int& Vector::at(size_t pos) const { return arr[pos]; }



If you return a pointer or reference from a data structure, you can flag the return value as const so that it can't be used to change the data structure without going through the normal getters or setters. I don't use this very often, as I don't like passing out pointers to the inner workings of my data structures, but it can be useful if you have a constant vector, say.


Finally, I talked a bit about deleting Copy Constructors and Assignment Operators (operator=). The Rule of 3 states that if you need any of these three things you probably need all three of them: Destructor, Copy Constructor, Assignment Operator. *However*, if you delete the copy constructor and assignment operator (as I do in the video) then you only really need a destructor.


This has some advantages (in addition to being lazy), namely that in a lot of cases you don't want your data structure to be duplicated by accident. If you delete your copy constructor and assignment operator, then you won't accidentally duplicate a GB of memory if you pass it to a function that you made call by value by accident. The compiler will refuse to compile the code, which IMO is a good thing. If I need to, I'll make a clone function that will duplicate all the elements in it, but oftentimes just deleting them is good enough for my needs.

Deleting copy constructors and assignment operators







Tags:
csci 41
rule of 3
const
c++