
Page 1 of 31. Showing 310 results (0.008 seconds)
STR05-C. Use pointers to const when referring to string literals
) are notionally constant and should consequently be protected by const qualification. This recommendation is a specialization of DCL00-C. Const-qualify immutable objects and also supports STR30-C. Do not attempt to modify string literals. Adding const qualification may propagate through a program; as const qualifiersEXP05-C. Do not cast away a const qualification
Do not cast away a const qualification on an object of pointer type. Casting away the const qualification allows a program to modify the object referred … 9899:2011] provides a footnote (subclause 6.7.3, paragraph 4): The implementation may place a const object that is not volatile in a read-only region of storageDCL00-C. Const-qualify immutable objects
Immutable objects should be const-qualified. Enforcing object immutability using const qualification helps ensure the correctness and security of applications … ]. STR05-C. Use pointers to const when referring to string literals describes a specialized case of this recommendation. Adding const qualification may propagateDCL13-C. Declare function parameters that are pointers to values not changed by the function as const
Declaring function parameters const indicates that the function promises not to change these values. In C, function arguments are passed by value rather than … programmers assume a function will not change its arguments and that declaring the function's parameters as const is unnecessary. void foo(int x) { x = 3; /* VisibleDCL52-CPP. Never qualify a reference type with const or volatile
C++ does not allow you to change the value of a reference type, effectively treating all references as being const qualified. The C++ Standard, [dcl.ref … of a reference type. Only a value of non-reference type may be cv-qualified. When attempting to const-qualify a type as part of a declaration that uses referenceSTR30-C. Do not attempt to modify string literals
be assigned only to pointers to (or arrays of) const char or const wchar_t. It is unspecified whether these arrays of string literals are distinct from each … violation because string literals are typically stored in read-only memory. (See undefined behavior 32.) Avoid assigning a string literal to a pointer to non-constSTR51-CPP. Do not attempt to create a std::string from a null pointer
::length(): basic_string::basic_string(const charT *, const Allocator &) basic_string &basic_string::append(const charT *) basic_string &basic_string::assign(const charT *) basic_string &basic_string::insert(size_type, const charT *) basic_string &basic_string::replace(size_type, size_type, const charTEXP40-C. Do not modify constant objects
The C Standard, 6.7.4, paragraph 7 [ISO/IEC 9899:2024], states If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined. See also undefined behavior 61. There are existing compiler implementations that allowOOP51-CPP. Do not slice derived objects
. #include <iostream> #include <string> class Employee { std::string name; protected: virtual void print(std::ostream &os) const { os << "Employee: " << get_name() << std::endl; } public: Employee(const std::string &name) : name(name) {} const std::string &get_name() const { return name; } friendMSC09-C. Character encoding: Use subset of ASCII for safety
() will include it: char myFilename[1000]; const char elimNewLn[] = "\n"; fgets(myFilename, sizeof(myFilename)-1, stdin); myFilename[sizeof(myFilename)-1] = '\0 … file names that violate the guidelines for selecting safe characters: char myFilename[1000]; const char elimNewln[] = "\n"; const char badChars[] = "-\n\r