Advanced Classes
- Static methods.
- Static allows you to create a member that is shared across all instances of a class.
- We will focus on static member functions for now.
- These member functions do not have a this pointer.
- But they can access protected and private members if they are passed to the function.
- You can call them without creating an instance of the class.
- Static Data
- Again, this is a value shared across all members of a class.
- You need to declare the variable outside the class before c++17
- This is usually done in the implementation file.
- Lifetime for static data is from when it is declared to the end of the program.
- This is essentially a global variable in the namespace of the class.
- In c++17 and beyond you can declare a variable to be static inline
- This allocates the space automatically.
- You can declare member data to be const as well
- But it must be initialized in c_tor initializer or in class initializers
- We have discussed const members in the past
- Static members can not be const.
- But this makes sense as they don't have any data to change.
- Your author is a big fan of declaring a function const when it is.
- Please see staticDemo.cpp
- Mutable members
- If you have member data that might change in a const method
- For example record keeping, how many times has the cell been accessed in a spreadsheet.
- Or perhaps the current pointer in the DLCL class
- They can be marked mutable and thus changed in const functions.