C++ Const and Static Members
A const member function is a method that promises not to change its object. A const object can call only const member functions.
A const object calls a const getter
The getPages method is marked const. The const object b can call it, and the page count prints.
#include <iostream>
#include <string>
class Book {
public:
std::string title;
int pages;
Book(std::string t, int p) {
title = t;
pages = p;
}
int getPages() const {
return pages;
}
void setPages(int p) {
pages = p;
}
};
int main() {
const Book b("Dune", 412);
std::cout << b.getPages() << std::endl;
return 0;
}A non-const method on a const object
This program calls setPages on a const object. setPages is not marked const, so the compiler refuses the program and reports the error line below.
#include <iostream>
#include <string>
class Book {
public:
std::string title;
int pages;
Book(std::string t, int p) {
title = t;
pages = p;
}
int getPages() const {
return pages;
}
void setPages(int p) {
pages = p;
}
};
int main() {
const Book b("Dune", 412);
b.setPages(500);
std::cout << b.pages << std::endl;
return 0;
}A static counter counts live objects
The constructor of Box adds 1 to the shared counter and the destructor takes 1 away. The prints show 0, then 2 inside the scope, then 0 again.
#include <iostream>
class Box {
public:
static int alive;
Box() {
alive++;
}
~Box() {
alive--;
}
};
int Box::alive = 0;
int main() {
std::cout << "Alive: " << Box::alive << std::endl;
{
Box a;
Box b;
std::cout << "Alive: " << Box::alive << std::endl;
}
std::cout << "Alive: " << Box::alive << std::endl;
return 0;
}A static member function reads the counter
The count function is static, so main calls it through the class name with no object. It reads the shared counter at three moments.
#include <iostream>
class Box {
public:
static int alive;
Box() {
alive++;
}
~Box() {
alive--;
}
static int count() {
return alive;
}
};
int Box::alive = 0;
int main() {
std::cout << "Count: " << Box::count() << std::endl;
{
Box a;
std::cout << "Count: " << Box::count() << std::endl;
}
std::cout << "Count: " << Box::count() << std::endl;
return 0;
}Example
#include <iostream>
#include <string>
class LibraryBook {
public:
static int alive;
std::string title;
LibraryBook(std::string t) {
title = t;
alive++;
}
~LibraryBook() {
alive--;
}
std::string getTitle() const {
return title;
}
static int count() {
return alive;
}
};
int LibraryBook::alive = 0;
int main() {
const LibraryBook first("Dune");
std::cout << first.getTitle() << std::endl;
{
LibraryBook second("Emma");
std::cout << second.getTitle() << std::endl;
std::cout << "In library: " << LibraryBook::count() << std::endl;
}
std::cout << "In library: " << LibraryBook::count() << std::endl;
return 0;
}Key points
- The word const sits after the parameter list of a member function.
- A const member function can read members but cannot change them.
- A const object can call const member functions only.
- A static member is shared by all objects of a class.
- A static member exists even before any object is made.
- A static member function has no object, so it can reach static members only.
- A static counter can count how many objects of a class are alive.
In this example, the const book first prints its title through the const getter getTitle. A second book joins inside the scope, and the static count prints 2. The scope ends, the second book is destroyed, and the count prints 1.
Note
A static data member is defined once outside the class body.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What can a const object call?