COMP 206 Unit 9: Name Control

Description

University-level C++ COMP 206 Flashcards on COMP 206 Unit 9: Name Control, created by Adriana Vincelli-Joma on 27/09/2021.
Adriana Vincelli-Joma
Flashcards by Adriana Vincelli-Joma, updated more than 1 year ago
Adriana Vincelli-Joma
Created by Adriana Vincelli-Joma about 3 years ago
1
0

Resource summary

Question Answer
static 1. allocated once at fixed address; object created in static data area 2. local to particular translation unit: - visibility: name cannot be seen outside translation unit/class - linkage: determines what names linker will see
static keyword for variables inside function object is initialized only once, first time function is called, and then retains value between function invocations
static keyword for class objects inside function - some initialization required for object - assignment to 0 has meaning only for built-in types; user-defined types must be initialized with constructor calls - class must have default constructor
static object destructor destroys all objects with static storage; called when main() exits or when Standard C library function exit() is explicitly called -destruction occurs in reverse order -only constructed objects are destroyed - when main() exits, destructors for objects that have been constructed are called in reverse order of construction
auto - indicates local variable - refers to way compiler automatically allocates storage for variable
register indicates to compiler that object should be stored in machine register
namespace - puts name of members in distinct space - declarative region that provides scope to identifiers
keyword namespace used to control linkage - linkage describes how names can refer to same entity throughout whole program or single translation unit - names of global functions, global variables, and classes are still in single global namespace - subdivide global name space into more manageable pieces using namespace
external linkage link time the name is visible to the linker everywhere, external to that translation unit
internal linkage - local to translation unit - use same name in other translation units without name clash
creating namespace - similar to creation of class - definition can appear only at global scope, or nested within another namespace - no terminating semicolon is necessary after closing brace of namespace definition - can be "continued" over multiple header files using syntax that, for a class, would appear to be redefinition
namespace example namespace MyLib { // Declarations } int main() { }
steps in using namespace - specifying name using scope resolution operator - using directive to introduce all names in namespace - using declaration to introduce names one at a time
using directive - provides access to all namespace qualifiers and scope operators - apply using keyword to namespace identifiers
using declaration - inject names one at a time into current scope - declaration within current scope
static data members vs global variables - global data can be modified by anyone, and its name can clash with other identical names in large project - static data belong to class; its name is scoped inside class and it can be public, private, or protected
storage utilization for static data members - storage must be defined in single place - definition must occur outside class and only one definition is allowed
initialize static array - syntax for initializing static arrays is same as for any aggregate, including automatic counting - arrays, non-integral and non-const statics must be initialized externally
restrictions placed on static data members and local classes - put static data members in classes that are nested inside other classes - cannot have static data members inside local classes
static member functions - work for class as whole rather than for particular object of class; express association with particular class - only access static data members
steps to create and use static member function - create function with static keyword in front of function name (e.g. static void f() {};) - call function with dot or arrow in association with object
Guarantees of order of creation/destruction of static objects - order of initialization of static objects is guaranteed to be order in which object definitions appear in translation unit - order of destruction is guaranteed to be reverse of order of initialization - no guarantee concerning order of initialization of static objects across translation units - linking-loading mechanism guarantees static initialization to zero before dynamic initialization specified by programmer takes places
3 approaches to resolving problems from creation/destruction of static objects 1. avoid static initialization dependencies 2. put critical static objects definitions in single file 3. use programmatic techniques to scatter static objects across translation units
handling static objects using Technique 1 - requires additional class in library header file - class is responsible for dynamic initialization of library's static object - declarations for objects do not allocate storage - have pointers to objects and create them using new inside constructor
handling static objects using Technique 2 - static objects inside functions are initialized first time function is called - place static object function that returns reference to object -first time function is called, forces initialization to take place - define wrapping function in separate files
alternate linkage specification - escape mechanism provided in C++ - produced in language by overloading extern keyword
using alternate linkage specification - extern is followed by string that specifies linkage you want for declaration (e.g. extern "C" float f(int a, char b);) - groups declarations with alternate linkage inside braces or header file
Show full summary Hide full summary

Similar

COMP 206 Unit 5: Hiding the Implementation
Adriana Vincelli-Joma
COMP 206 Unit 6: Initialization and Cleanup
Adriana Vincelli-Joma
COMP 206 Unit 7: Function Overloading and Default Arguments
Adriana Vincelli-Joma
COMP 206 Unit 8: Constants
Adriana Vincelli-Joma
COMP 206 Unit 11: Operator Overloading
Adriana Vincelli-Joma
COMP 206 Unit 12: Dynamic Object Creation
Adriana Vincelli-Joma
COMP 206 Unit 13: Inheritance and Composition
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
GOVN 377 Unit 6: New Technology: Data Mining, Matching, and Management
Adriana Vincelli-Joma
GOVN 377 Unit 7: Research and the Implications for Information Access adn Privacy
Adriana Vincelli-Joma