[补充]C++存储修饰符--生存空间详细解释

原创 2002年02月22日 13:02:00

 

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 i is in a block enclosed by curly braces, i has 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 x and y are 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++提供下面几个存储空间修饰(storageclassspeci...
  • zosatapo
  • zosatapo
  • 2002年02月21日 23:59
  • 1301

Vue.js学习系列(二十一)--修饰符

在事件处理程序中调用event.preventDefault() 或 event.stopPropagation()是非常常见的需求。尽管我们可以在methods 中轻松实现这点,但更好的方式是:me...
  • bwf_erg
  • bwf_erg
  • 2017年03月15日 19:31
  • 231

C++存储修饰符解释 zosatapo(原作)

C++存储修饰符解释(纯理论部分)在C++中,存储空间主要是指变量或者函数或者类(下文中为了方便都称为标识符)的可见性和使用范围。为此C++提供下面几个存储空间修饰(storageclassspeci...
  • Flyincloud
  • Flyincloud
  • 2002年02月26日 09:00
  • 577

C++修饰符一些概念

C++辨析系列谈本文主要是参考网上的一篇文章,对C++中的一些重要修饰符和函数(static,const,inline,虚函数)进行分析,一方面可以加深对C++的认识和理解;另一方面也是抛砖引玉,希望...
  • qiuyq
  • qiuyq
  • 2005年05月29日 11:24
  • 1474

C++中各种类型修饰符汇总

explicit C++中, 一个参数的构造函数(或者除了第一个参数外其余参数都有默认值的多参构造函数), 承担了两个角色。 1 是个构造器 ,2 是个默认且隐含的类型转换操作符。 所以, 有...
  • lylyyg
  • lylyyg
  • 2015年08月13日 09:51
  • 1804

C++修饰符----继承

继承的特点: public,protected,private三种继承方式,相应的也改变了基类成员的访问属性: 1、public继承:基类public成员、protected成员、private成...
  • dong6dong6
  • dong6dong6
  • 2017年08月05日 13:51
  • 128

C/C++常见修饰符

auto, const, register,  static, volatile, extern.
  • he11ohell0
  • he11ohell0
  • 2015年06月08日 00:43
  • 1803

C++精进篇(三)之―常量变量等修饰符

一、Const常量 1、Const定义: 常类型是指使用类型修饰符const修饰的类型,常类型的变量或对象的值是不能被更新的。const 推出的初始目的,正是为了取代预编译指令,消除它的缺点,同时...
  • fanyun_01
  • fanyun_01
  • 2016年03月25日 22:18
  • 953

C++中的访问修饰符

源于牛客网上的一道题目,下列程序编译时会出现错误,请根据行号选择错误位置( ): #include using namespace std; class A{ int a1; prot...
  • x_i_y_u_e
  • x_i_y_u_e
  • 2016年09月06日 10:36
  • 932

存储类型修饰符的区别(auto、static、register、extern、volatile、restrict)

转自:http://blog.csdn.net/setflvet/article/details/6924878 一、局部变量和全局变量: (1)局部变量:局部变量也叫自动变量,它声明在函数开始,...
  • dddd0216
  • dddd0216
  • 2016年06月09日 15:10
  • 492
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[补充]C++存储修饰符--生存空间详细解释
举报原因:
原因补充:

(最多只允许输入30个字)