Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Why does C++ make objects unmodifiable by default?

Shulou Source: shulou.com Published: 2022-06-01 20:48:24 10月01日 Update

This article mainly explains why C++ makes objects unmodifiable by default. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn why C++ makes objects unmodifiable by default.

Con.1: make objects unmodifiable by default

Reason (reason)

Immutable objects are easier to reason about, so make objects non-const only when there is a need to change their value. Prevents accidental or hard-to-notice change of value.

Immutable objects are easier to understand, so objects are defined as non-constants only when there is a need for change. Prevent accidental or imperceptible modification of the value of the object.

Example (sample)

For (const int i: C) cout

Tags: Objects situations functions parameters Centers contents variables local learning practical deeper not easy interest reasons only practicality practicality constants suggestions simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Shulou Technology OPPO Reno MySQL