|
|
By Alex, on September 14th, 2007
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
By Alex, on September 11th, 2007
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
By Alex, on September 11th, 2007
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
By Alex, on September 7th, 2007
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)
By Alex, on September 6th, 2007
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
|
|