Karteikarten
COMP 206 Unit 8: Constants
Karten in diesem Stapel (28)
- 1preprocessor substitution- #define BUFSIZE 100
- use bufsize anyplace where compiler must know value at compile time - 2BUFSIZEonly 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
- 3constant foldingcompiler will reduce complicated constant expression to simple one by performing necessary calculations at compile time
- 4const 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 - 5internal linkagevisible only within file where it is defined and cannot be seen at link time by other translation units
- 6const 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 - 7C handling const- defaults to external linkage for consts
- const always creates storage
- const is always a definition, no initializer necessary - 8C++ handling const- defaults to internal linkage for consts
- const doesn't necessarily create storage
- requires const definition to have initializer - 9making pointer const1. const can be applied to what pointer is pointing to
2. const can be applied to address stored in pointer itself - 10const int* uu is pointer which points to const int
- 11int const* vv is ordinary pointer to an int that happens to be const
- 12int d = 1 int* const w = &dw is pointer, which is const, that points to an int
- 13const pointercompiler requires that pointer be given initial value that will be unchanged for life of that pointer
- 14type checking enforced when using const1. assign address of non-const object to const pointer
2. can't assign address of const object to non-const pointer - 15when type checking not enforced when using constcharacter array literals (const character arrays)
- 16passing by const value- original value of variable will not be changed by function
- argument cannot be changed - 17returning 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) - 18temporary objectscompiler is responsible for deciding that they're needed and details of their existence; automatically const
- 19passing addresses- make const if at all
- allows function to change value of argument - 20returning addressesreturn address of variable, not literal or expression
- 21argument passingpassing info. from calling function to called function
- 22use of const in class- constant for lifetime of object
- allocates storage within each object and represents value that is initialized once and cannot change - 23constructor initializer listlist 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) {} - 24constructor for built-in type1. class B {
int i;
public:
B(int ii);
void print();
};
2. class Integer {
int i;
public:
Integer(int ii = 0);
void print();
}; - 25static 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 - 26const objecte.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 - 27volatile- data may change outside knowledge of compiler
- tells compiler not to make any assumptions about data, especially during optimization - 28const volatilecan't be changed by client programmer but instead change through some outside agency
Informationen
- Beschreibung
- University-level C++ COMP 206 Karteikarten am COMP 206 Unit 8: Constants, erstellt von Adriana Vincelli-Joma am 22/09/2021.
- Erstellt von
- Adriana Vincelli-Joma
- Aufrufe
- 18
- Tags
- Bewertung