第七讲:作用域.可见性.生存期.全局对象

来源:互联网 发布:linux dma中断 编辑:程序博客网 时间:2024/06/05 20:03

  • 函数原形的作用域
    函数原型中的参数,其作用域始于( 结束于 )
    double area (double radius);

  • 局部作用域
    函数的形参,在块中声明的标识符,其作用域自声明处起,限于块中.{};

  • 类作用域
    类作用域作用于特定的成员名.
    类X的成员m具有类作用域,对于m的访问方式如下

    1. 如果在X的成员函数中没有声明同名的局部作用域标识符,那么在该函数内可以访问成员m
    2. 通过表达式X.m或者X::m访问
    3. 通过表达式ptr->m
  • 命名空间
    命名空间可以解决类名.函数名等的命名冲突
    命名空间的声明
    namespace 命名空间名{
    各种声明(函数声明.类声明...
    class SomeClass{.};

    特殊的命名空间

    1. 全局命名空间:默认的命名空间
    2. 匿名命名空间:对每个源文件是惟一的
  • 命名空间作用域
    一个命名空间确定了一个命名空间作用域
    引用其它命名空间作用域中的标识符
    命名空间::标识符名
    例如声明一个someclass型的对象
    someNs::SomeClass obj1;
    将其他命名空间作用域的标识符暴露于当前作用域
    对指定标识符
    using 命名空间名::标识符名
    对所有标识符
    using namespace 命名空间名;


  • 可见性
    可见性是从对标识符的引用的角度来谈的概念
    可见性表示从内层作用域向外层作用域"看"时能看见什么.
    如果标识在某处可见,则就可以在该处引用此标识符

标识符应声明在前,引用在后.
如果某个标识符在外层中声明,且在内层中没有同一标识符的声明,则该标识符在内层可见.对于两个嵌套的作用域,如果在内层作用域内声明了与外层同名的则在内层不可见.

  • 同一作用域中的同名标识符
    在同一作用域内的对象名,函数名,枚举常量名会隐藏同名的类名或枚举类型名.
    重载的函数可以有相同的函数名.
#include <iostream>using namespace std;int i;      //在全局命名空间中的全局变量namespace Ns {    int j;      //在Ns命名空间中的全局变量}int main() {    i = 5;          //为全局变量i赋值    Ns::j = 6;      //为全局变量j赋值    {               //子块1        using namespace Ns;  //当前块中可以直接引用Ns中的标识符        int i;      //局部变量,局部作用域        i = 7;        cout << "i = " << i << endl;//输出7        cout << "j = " << j << endl;//输出6    }    cout << "i = " << i << endl;    //输出5    return 0;}

  • 对象的生存期
    对象从产生到结束的这段时间就是它的生存期,在对象生存期内,对象将保持它的值,知道被更新为止.

-静态生存期
生存期与程序的运行期相同
在文件作用域中声明的对象具有这种生存期
在函数内部声明静态生存期对吸纳该,要冠以关键字static.

#include<iostream>using namespace std;int i = 5;   //文件作用域int main() {     cout << "i=" << i << endl;     return 0;}i具有静态生存期
  • 动态生存期
    块作用域中声明的,没有用static修饰的对象是动态生存期的对象(习惯称为局部生存期对象)
    开始于程序执行到声明点时,结束于命名该标识符的作用域结束处.
#include <iostream>using namespace std;void fun();int main() {  fun();  fun();}void fun() {  static int a=1;  int i=5;  a++;  i++;  cout<<"i="<<i<<",a="<<a<<endl;}i= 6, a=2;i= 6, a=3;

变量的生存期与可见性

#include<iostream>using namespace std;int i = 1; // i 为全局变量,具有静态生存期。void other() {  static int a = 2;  static int b;   // a,b为静态局部变量,具有全局寿命,局部可见。   //只第一次进入函数时被初始化。  int c = 10; // C为局部变量,具有动态生存期,            //每次进入函数时都初始化。  a += 2; i += 32; c += 5;  cout<<"---OTHER---\n";  cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;  b = a;}int main() {  static int a;// 静态局部变量,有全局寿命,局部可见。  int b = -10; // b, c为局部变量,具有动态生存期。  int c = 0;    cout << "---MAIN---\n";  cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;  c += 8; other();  cout<<"---MAIN---\n";  cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;  i += 10; other();      return 0;}运行结果:---MAIN--- i: 1 a: 0 b: -10 c: 0---OTHER--- i: 33 a: 4 b: 0 c: 15---MAIN--- i: 33 a: 0 b: -10 c: 8---OTHER--- i: 75 a: 6 b: 4 c: 15

具有静态、动态生存期对象的时钟程序

#include<iostream>using namespace std;class Clock {   //时钟类定义public: //外部接口    Clock();    void setTime(int newH, int newM, int newS);   //三个形参均具有函数原型作用域    void showTime();private:    //私有数据成员    int hour, minute, second;};Clock::Clock() : hour(0), minute(0), second(0) { }  //构造函数void Clock::setTime(int newH, int newM, int newS) {//三个形参均具有局部作用域    hour = newH;    minute = newM;    second = newS;}void Clock::showTime() {    cout << hour << ":" << minute << ":" << second << endl;}Clock globClock;//声明对象globClock,                //具有静态生存期,文件作用域int main() { //主函数    cout << "First time output:" << endl;       //引用具有文件作用域的对象:    globClock.showTime();//对象的成员函数具有类作用域    globClock.setTime(8,30,30);     Clock myClock(globClock);         //声明具有块作用域的对象myClock    cout<<"Second time output:"<<endl;      myClock.showTime(); //引用具有块作用域的对象    return 0;}程序的运行结果为:First time output:0:0:0Second time output:8:30:30

  • 数据与函数
    数据存储在局部对象中,通过参数传递实现共享--函数间的参数传递
    数据存储在全局对象中
    将数据和使用数据的函数封装在类中

  • 使用全局对象

#include<iostream>using namespace std;int global;void f() { global=5; }void g() { cout << global << endl; }int main() {   f();   g(); //输出“5”   return 0;}#include<iostream>using namespace std;class Application { public:   void f();   void g(); private:   int global;};void Application::f() {  global = 5;}void Application::g() {  cout << global << endl;}int main() {   Application  MyApp;   MyApp.f();   MyApp.g();   return 0;}
  • 静态成员
    1. 静态数据成员
      用关键字static声明
      该类的所有对象维护该成员的同一个拷贝
      必须在类外定义和初始化,用(::)来指明所属的类。
    2. 静态成员函数
      类外代码可以使用类名和作用域操作符来调用静态成员函数。
      静态成员函数只能引用属于该类的静态数据成员或静态成员函数。

说明:
1)在为对象分配空间时,不分配静态数据成员的空间,因为它不属于任何对象。只要类中定义了静态数据成员,即使不定义对象,编译系统也要为静态数据成员开辟内存空间。
2)静态数据成员不随对象的建立而分配空间,也不随对象的撤消而释放空间,其值被保留。静态数据成员在程序被编译时就分配了空间,在程序结束时,才释放空间。
3)静态数据成员可以被初始化,但只能在类体之外初始化,格式: 数据类型 类名::静态数据成员名 = 初值;
4)不能用参数初始化表对静态数据成员初始化,例如:
Box(int h,int w,int len): height(h){ } //错误

2、静态成员函数
在类的定义中,成员函数前加 static 限定词,该成员函数就成为静态成员函数。例:
static int volume( );
用途:静态成员函数的作用不是为了对象之间的沟通,主要是为了引用本类中的静态数据成员。它可以直接引用本类的静态数据成员。
静态成员函数与普通成员函数的区别:静态成员函数没有 this 指针,所以静态成员函数不能访问本类中的非静态数据成员。

例如具有静态数据成员的 Point类

#include <iostream>using namespace std;class Point {public:     Point(int x=0, int y=0) : x(x), y(y) { count++; }    Point(Point &p);        int getX() { return x; }    int getY() { return y; }    void showCount() {      cout << " Object count=“ << count << endl;  }private:        int x,y;    static int count;};Point::Point(Point &p) {    x = p.x;    x = p.y;    count++;}int Point::count=0; int main() {    Point a(4,5);       cout<<"Point A:"<<a.getX()<<","<<a.getY();    a.showCount();      Point b(a);     cout<<"Point B:"<<b.getX()<<","<<b.getY();    b.showCount();      return 0;}

静态成员函数举例

#include <iostream>using namespace std;class Application {public:   static void f();    static void g();private:   static int global;};int Application::global=0;void Application::f() {    global=5;}void Application::g() {    cout << global << endl;}int main() {    Application::f();    Application::g();    return 0;}class A {public:    static void f(A a);private:    int x;};void A::f(A a) {    cout << x;   //对x的引用是错误的    cout << a.x; //正确}
#include <iostream>using namespace std;class Point {   //Point类定义public: //外部接口    Point(int x = 0, int y = 0) : x(x), y(y) { count++; }    Point(Point &p);    ~Point() {  count--; }    int getX() { return x; }    int getY() { return y; }    static void showCount() {   //静态函数成员      cout << "  Object count = " << count << endl;    }private:    //私有数据成员    int x, y;    static int count;   //静态数据成员声明};Point::Point(Point &p) {    x = p.x;    y = p.y;    count++;}int Point::count=0; int main() { //主函数实现    Point a(4,5);   //声明对象A    cout<<"Point A,"<<a.getX()<<","<<a.getY();    Point::showCount(); //输出对象个数    Point b(a); //声明对象B    cout<<"Point B,"<<b.GetX()<<","<<b.GetY();    Point:: showCount();    //输出对象个数    return 0;}
原创粉丝点击