Navigation



8.11 — Static member variables

Static keyword in C

In the lesson on file scope and the static keyword, you learned that static variables keep their values and are not destroyed even after they go out of scope. For example:

int GenerateID() { static int s_nID = 0; return s_nID++; } int main() { std::cout << GenerateID() << std::endl; . . . → Read More: 8.11 — Static member variables

8.10 — Const class objects and member functions

In the lesson on passing arguments by reference, we covered the merits of passing function parameters as const variables. To recap, making variables const ensures their values are not accidentally changed. This is particularly important when passing variables by reference, as callers generally will not expect the values they pass to a function to . . . → Read More: 8.10 — Const class objects and member functions

8.9 — Class code and header files

Defining member functions outside the class definition

All of the classes that we have written so far have been simple enough that we have been able to implement the functions directly inside the class definition itself. For example, here’s our ubiquitous Date class:

class Date { private: int m_nMonth; int m_nDay; int m_nYear; Date() . . . → Read More: 8.9 — Class code and header files

8.8 — Constructors (Part II)

Private constructors

Occasionally, we do not want users outside of a class to use particular constructors. To enforce this behavior, we can make constructors private. Just like regular private functions, private constructors can only be accessed from within the class. Let’s take a look at an example of this:

class Book { private: int . . . → Read More: 8.8 — Constructors (Part II)

8.7 — The hidden “this” pointer

One of the big questions that new programmers often ask is, “When a member function is called, how does C++ know which object it was called on?”. The answer is that C++ utilizes a hidden pointer named “this”! Let’s take a look at “this” in more detail.

The following is a simple class that . . . → Read More: 8.7 — The hidden “this” pointer