COMP 206 Unit 8: Constants

Descripción

University-level C++ COMP 206 Fichas sobre COMP 206 Unit 8: Constants, creado por Adriana Vincelli-Joma el 22/09/2021.
Adriana Vincelli-Joma
Fichas por Adriana Vincelli-Joma, actualizado hace más de 1 año
Adriana Vincelli-Joma
Creado por Adriana Vincelli-Joma hace alrededor de 3 años
2
0

Resumen del Recurso

Pregunta Respuesta
preprocessor substitution - #define BUFSIZE 100 - use bufsize anyplace where compiler must know value at compile time
BUFSIZE only exists during preprocessing; doesn't occupy storage and can be placed in header file to produce single value for all translation units that use it
constant folding compiler will reduce complicated constant expression to simple one by performing necessary calculations at compile time
const in header file - define constant: #define MAX 10 const int MAX = 10; - must always assign value to const when you define it -const in C++ defaults to internal linkage
internal linkage visible only within file where it is defined and cannot be seen at link time by other translation units
const and aggregates - const value cannot be used at compile time because compiler is not required to know contents of storage at compile time - compiler must be able to generate code that moves stack pointer to accommodate array
C handling const - defaults to external linkage for consts - const always creates storage - const is always a definition, no initializer necessary
C++ handling const - defaults to internal linkage for consts - const doesn't necessarily create storage - requires const definition to have initializer
making pointer const 1. const can be applied to what pointer is pointing to 2. const can be applied to address stored in pointer itself
const int* u u is pointer which points to const int
int const* v v is ordinary pointer to an int that happens to be const
int d = 1 int* const w = &d w is pointer, which is const, that points to an int
const pointer compiler requires that pointer be given initial value that will be unchanged for life of that pointer
type checking enforced when using const 1. assign address of non-const object to const pointer 2. can't assign address of const object to non-const pointer
when type checking not enforced when using const character array literals (const character arrays)
passing by const value - original value of variable will not be changed by function - argument cannot be changed
returning by const value - return value is const (original value inside function frame will not be modified) - if function returns class object by value as const, return value of function cannot be lvalue (cannot be assigned to or otherwise modified)
temporary objects compiler is responsible for deciding that they're needed and details of their existence; automatically const
passing addresses - make const if at all - allows function to change value of argument
returning addresses return address of variable, not literal or expression
argument passing passing info. from calling function to called function
use of const in class - constant for lifetime of object - allocates storage within each object and represents value that is initialized once and cannot change
constructor initializer list list of "constructor calls" that occur after function argument list and colon, but before opening brace of constructor -e.g Fred::Fred(int sz) : size(sz) {}
constructor for built-in type 1. class B { int i; public: B(int ii); void print(); }; 2. class Integer { int i; public: Integer(int ii = 0); void print(); };
static const - only one instance, regardless of how many objects of class are created - member of class which is constant, and which cannot change from one object of class to another
const object e.g. const int i = 1; const blob b(2); - for compiler to enforce constness, it must ensure that no data members of object are changed during object's lifetime - to declare member function const, tell compiler function can be called for const object
volatile - data may change outside knowledge of compiler - tells compiler not to make any assumptions about data, especially during optimization
const volatile can't be changed by client programmer but instead change through some outside agency
Mostrar resumen completo Ocultar resumen completo

Similar

COMP 206 Unit 11: Operator Overloading
Adriana Vincelli-Joma
COMP 206 Unit 9: Name Control
Adriana Vincelli-Joma
COMP 206 Unit 6: Initialization and Cleanup
Adriana Vincelli-Joma
COMP 206 Unit 13: Inheritance and Composition
Adriana Vincelli-Joma
COMP 206 Unit 5: Hiding the Implementation
Adriana Vincelli-Joma
COMP 206 Unit 12: Dynamic Object Creation
Adriana Vincelli-Joma
COMP 206 Unit 7: Function Overloading and Default Arguments
Adriana Vincelli-Joma
COMP 206 Unit 3: The C in C++
Adriana Vincelli-Joma
GOVN 377 Unit 8: The Brave New World of Data Management and Manipulation
Adriana Vincelli-Joma
HIST 404: Unit 4 Science in the Renaissance
Adriana Vincelli-Joma
HIST 404: Unit 9 Discovering DNA
Adriana Vincelli-Joma