-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconst-method-and-mutable.cpp
More file actions
66 lines (48 loc) · 1.57 KB
/
const-method-and-mutable.cpp
File metadata and controls
66 lines (48 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
// const method and mutable
// -
//
// Key notes:
// - const method: cannot modify member variables (except mutable)
// - const method: can only call other const methods
// - always mark methods const if they don't modify state
TEST_CASE("con-met-1") {
//// 1. basic
struct Circle {
double radius;
double area() const { // ✓ does not modify object
return 3.14 * radius * radius;
}
void setRadius(double r) { // non-const — modifies object
radius = r;
}
};
const Circle c{5.0};
c.area(); // ✓ const method on const object
// c.setRadius(3); // ❌ non-const method on const object
//// 2. mutable — allow mutation inside const method
struct Cache {
mutable int hitCount = 0; // mutable = exempt from const
mutable bool cached = false;
int value;
int get() const {
hitCount++; // ✓ mutable — allowed in const method
return value;
}
};
//// 3. const method with pointer member
struct Foo {
int* ptr;
void fn() const {
*ptr = 42; // ✓ modifying pointed-to value — allowed
// ptr = nullptr; // ❌ modifying the pointer itself — not allowed
}
};
}
}