Example
struct Request { static int count; int number; Request { number = count; // modifies the instance variable "this->number" ++count; // modifies the class variable "Request::count" } }; int Request::count = 0;In this C++ example, the class variable Request::count
is incremented on each call to the constructor, so that Request::count
always holds the number of Requests that have been constructed, and each new Request object is given a number
in sequential order. Since count
is a class variable, there is only one object Request::count
; in contrast, each Request object contains its own distinct number
field.
Read more about this topic: Class Variable
Famous quotes containing the word example:
“Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.”
—Marcel Proust (18711922)