[补充]C++存储修饰符--生存空间详细解释
2002-02-22 13:02
393 查看
C++ names can be used only in certain regions of a program. This area is called the “scope” of the name. Scope determines the “lifetime” of a name that does not denote an object of static extent. Scope also determines the visibility of a name, when class constructors and destructors are called, and when variables local to the scope are initialized. There are five kinds of scope:
Local scope. A name declared within a block is accessible only within that block and blocks enclosed by it, and only after the point of declaration. The names of formal arguments to a function in the scope of the outermost block of the function have local scope, as if they had been declared inside the block enclosing the function body. Consider the following code fragment:
{ int i; }
Because the declaration of
iis in a block enclosed by curly braces,
ihas local scope and is never accessible because no code accesses it before the closing curly brace.
Function scope. Labels are the only names that have function scope. They can be used anywhere within a function but are not accessible outside that function.
File scope. Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit after its declaration. Names with file scope that do not declare static objects are often called “global” names.
Class scope. Names of class members have class scope. Class member functions can be accessed only by using the member-selection operators (. or –>) or pointer-to-member operators (.* or –>*) on an object or pointer to an object of that class; nonstatic class member data is considered local to the object of that class. Consider the following class declaration:
class Point { int x; int y; };
The class members
xand
yare considered to be in the scope of class
Point.
Prototype scope. Names declared in a function prototype are visible only until the end of the prototype. The following prototype declares two names (
szDest,
szSource); these names go out of scope at the end of the prototype:
char *strcpy( char *szDest, const char *szSource );
相关文章推荐
- C++存储修饰符解释
- C++ 类里面,函数占用存储空间问题
- C++ 命名空间解释
- C/C++程序的存储空间布局
- c++_cast_最完整最详细的解释
- C/C++中存储类型修饰符的区别(auto、static、register、extern、volatile、restrict)
- C++之存储空间的动态分配和释放
- C/C++语言int和float内存存储分析与解释
- 详细解释强力的图片加载框架 Glide的配置(顺便补充下CollapsingToolbarLayout的一些功能)
- C++ 命名空间(namespace)的解释和使用
- c++学习读书笔记-指针和自由存储空间
- C++中的类所占内存空间总结(其中有一段关于成员函数处于代码段的解释) 2011-12-9 16:16
- c++学习之 指针和自由存储空间
- c++中字符串函数(过程)的详细解释
- KB和kB,MB和mb,详细解释计算机存储单位
- C/C++ 结构体 字节对齐原则详细举例解释 及sizeof的基本用法
- C与C++存储空间布局
- C/C++中的日期和时间time_t详细解释
- 【转】C++易混知识点1: 指针常量和常量指针的区别,附有详细案例解释
- 详细解释强力的图片加载框架 Glide的配置(顺便补充下CollapsingToolbarLayout的一些功能)