You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CppCoreGuidelines.md
+89-6Lines changed: 89 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -3216,6 +3216,8 @@ Class rule summary:
3216
3216
* [C.4: Make a function a member only if it needs direct access to the representation of a class](#Rc-member)
3217
3217
* [C.5: Place helper functions in the same namespace as the class they support](#Rc-helper)
3218
3218
* [C.7: Don't define a class or enum and declare a variable of its type in the same statement](#Rc-standalone)
3219
+
* [C.8: use `class` rather that `struct` if any member is non-public](#Rc-class)
3220
+
* [C.9: minimize exposure of members](#Rc-private)
3219
3221
3220
3222
Subsections:
3221
3223
@@ -3254,11 +3256,18 @@ Probably impossible. Maybe a heuristic looking for data items used together is p
3254
3256
3255
3257
##### Reason
3256
3258
3257
-
Ease of comprehension. The use of `class` alerts the programmer to the need for an invariant.
3259
+
Readability.
3260
+
Ease of comprehension.
3261
+
The use of `class` alerts the programmer to the need for an invariant.
3262
+
This is a useful convention.
3258
3263
3259
3264
##### Note
3260
3265
3261
-
An invariant is a logical condition for the members of an object that a constructor must establish for the public member functions to assume. After the invariant is established (typically by a constructor) every member function can be called for the object. An invariant can be stated informally (e.g., in a comment) or more formally using `Expects`.
3266
+
An invariant is a logical condition for the members of an object that a constructor must establish for the public member functions to assume.
3267
+
After the invariant is established (typically by a constructor) every member function can be called for the object.
3268
+
An invariant can be stated informally (e.g., in a comment) or more formally using `Expects`.
3269
+
3270
+
If all data members can vary independently of each other, no invariant is possible.
3262
3271
3263
3272
##### Example
3264
3273
@@ -3270,14 +3279,25 @@ An invariant is a logical condition for the members of an object that a construc
3270
3279
but:
3271
3280
3272
3281
class Date {
3282
+
public:
3283
+
Date(int yy, Month mm, char dd); // validate that {yy, mm, dd} is a valid date and initialize
3284
+
// ...
3273
3285
private:
3274
3286
int y;
3275
3287
Month m;
3276
3288
char d; // day
3277
-
public:
3278
-
Date(int yy, Month mm, char dd); // validate that {yy, mm, dd} is a valid date and initialize
3279
-
// ...
3280
3289
};
3290
+
3291
+
##### Note
3292
+
3293
+
If a class has any `private` data, a user cannot completely initialize an object without the use of a constructor.
3294
+
Hence, the class definer will provide a constructor and must specify its meaning.
3295
+
This effectivily means the definer need to define an invariant.
3296
+
3297
+
* See also [define a class with private data as `class`](#Rc-class).
3298
+
* See also [Prefer to place the interface first in a class](#Rl-order).
3299
+
* See also [minimize exposure of members](#Rc-private).
3300
+
* See also [Avoid `protected` data](#Rh-protected).
3281
3301
3282
3302
##### Enforcement
3283
3303
@@ -3387,6 +3407,63 @@ Mixing a type definition and the definition of another entity in the same declar
3387
3407
* Flag if the `}` of a class or enumeration definition is not followed by a `;`. The `;` is missing.
3388
3408
3389
3409
3410
+
### <a name="Rc-class"></a>C.8: use `class` rather that `struct` if any member is non-public
3411
+
3412
+
##### Reason
3413
+
3414
+
Readability.
3415
+
To make it clear that something is being hidden/abstracted.
3416
+
This is a useful convention.
3417
+
3418
+
##### Example, bad
3419
+
3420
+
struct Date {
3421
+
int d,m;
3422
+
3423
+
Date(int i, Month m);
3424
+
// ... lots of functions ...
3425
+
private:
3426
+
int y; // year
3427
+
};
3428
+
3429
+
There is nothing wrong with this code as far as the C++ language rules are concerned,
3430
+
but nearly everything is wrong from a design perspective.
3431
+
The private data is hidden far from the public data.
3432
+
The data is split in different parts of the class declaration.
3433
+
Different parts of the data has difference access.
3434
+
All of this decreases readability and complicates maintenance.
3435
+
3436
+
3437
+
##### Note
3438
+
3439
+
Prefer to place the interface first in a class [see](#Rl-order).
3440
+
3441
+
##### Enforcement
3442
+
3443
+
Flag classes declarate with `struct` if there is a `private` or `public` member.
3444
+
3445
+
3446
+
### <a name="Rc-private"></a>C.9: minimize exposure of members
3447
+
3448
+
##### Reason
3449
+
3450
+
Encapsulation.
3451
+
Information hiding.
3452
+
Mimimize the chance of untended access.
3453
+
This simplifies maintenance.
3454
+
3455
+
##### Example
3456
+
3457
+
???
3458
+
3459
+
##### Note
3460
+
3461
+
Prefer the order `public` members before `protected` members before `private` members [see](#Rl-order).
0 commit comments