第六章 类域

第六章 类域

一、类作用域

使用::访问全局变量

int x = 1;

namespace wd
{
int x = 20;

class Test
{
public:
    Test(int value)
    : x(value)
    {
        
    }

    void print(int x)
    {
        cout << "形参x = " << x << endl;     //4000
        cout << "数据成员x = " << this->x << endl;   //300
        cout << "数据成员x = " << Test::x << endl; 	 //300
        cout << "wd里面的x = " << wd::x << endl;	 //20
        cout << "全局x = " << ::x << endl;		//1
    }
private:
    int x;
};
}//end of namespace wd
int main(int argc, char **argv)
{
    wd::Test test(300);
    test.print(4000);
    return 0;
}

和函数一样,类的定义没有生存期的概念,但类定义有作用域和可见域。使用类名创建对象时,首要的前提是类名可见,类名是否可见取决于类定义的可见域,该可见域同样包含在其作用域中,类本身可被定义在3种作用域内,这也是类定义的作用域。

1.全局作用域

在函数和其他类定义的外部定义的类称为全局类,绝大多数的C++类是定义在该作用域中,我们在前面定义的所有类都是在全局作用域中,全局类具有全局作用域。

2.类作用域

一个类可以定义在另一类的定义中,这是所谓嵌套类或者内部类。举例来说,如果类A定义在类B中,如果A的访问权限是public,则A的作用域可认为和B的作用域相同,不同之处在于必须使用B::A的形式访问A的类名。当然,如果A的访问权限是private,则只能在类内使用类名创建该类的对象,无法在外部创建A类的对 象。

class Line
{
public:
    Line(int x1, int y1, int x2, int y2)
    : _pt1(x1, y1)
    , _pt2(x2, y2)
    {
        cout << "Line(int, int, int, int)" << endl;
    }

    void printLine() const
    {
        _pt1.print();
        cout << "--->";
        _pt2.print();
    }

    ~Line()
    {
        cout << "~Line()" << endl;
    }
    //内部类,嵌套类
private:
    class Point
    {
    public:
        Point(int ix = 0, int iy = 0)
        : _ix(ix)
        , _iy(iy)
        {
            cout << "Point(int, int)" << endl;
        }
    
        void print() const
        {
            cout << "(" << this->_ix
                 << ", " << this->_iy
                 <<")" << endl;
        }
    
        ~Point()
        {
            cout << "~Point()" << endl;
        }
    
    private:
        int _ix;
        int _iy;
    };
private:
    Point _pt1;
    Point _pt2;
};

void test()
{
    Line line(1, 2, 3, 4);
    line.printLine();

    /* cout << endl; */
    /* Line::Point pt1(12, 34); */
    /* pt1.print(); */
}
int main(int argc, char **argv)
{
    test();
    return 0;
}

附:设计模式之Pimpl

PIMPLPrivate ImplementationPointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。PIMPL又称作“编译防火墙”,它的实现中就用到了嵌套类。PIMPL设计模式有如下优点:

  1. 提高编译速度;
  2. 实现信息隐藏;
  3. 减小编译依赖,可以用最小的代价平滑的升级库文件;
  4. 接口与实现进行解耦;
  5. 移动语义友好
//Line.h
#ifndef __LINE_H__
#define __LINE_H_

//设计模式:PIMPL
class Line
{
public:
    Line(int, int, int, int);
    ~Line();
    void printLine() const;

    class LineImpl;//类的前向声明
private:
    LineImpl *_pimpl;
};

#endif

//Line.cc
#include "Line.h"
#include <stdlib.h>
#include <iostream>
#include <limits>

using std::cout;
using std::endl;

class Line::LineImpl
{
public:
    LineImpl(int x1, int y1, int x2, int y2)
    : _pt1(x1, y1)
    , _pt2(x2, y2)
    {
        cout << "LineImpl(int, int, int, int)" << endl;
    }

    void printLineImpl() const
    {
        _pt1.print();
        cout << "--->";
        _pt2.print();
    }

    ~LineImpl()
    {
        cout << "~LineImpl()" << endl;
    }
    //内部类,嵌套类
private:
    class Point
    {
    public:
        Point(int ix = 0, int iy = 0)
        : _ix(ix)
        , _iy(iy)
        {
            cout << "Point(int, int)" << endl;
        }
    
        void print() const
        {
            cout << "(" << this->_ix
                 << ", " << this->_iy
                 <<")" << endl;
        }
    
        ~Point()
        {
            cout << "~Point()" << endl;
        }
    
    private:
        int _ix;
        int _iy;
    };
private:
    Point _pt1;
    Point _pt2;
};
Line::Line(int x1, int y1, int x2, int y2)
: _pimpl(new LineImpl(x1, y1, x2, y2))
{
    cout << "Line(int, int, int, int)" << endl;
}

Line::~Line()
{
    cout << "~Line()" << endl;
    if(_pimpl)
    {
        delete _pimpl;
        _pimpl = nullptr;
    }
}

void Line::printLine() const
{
    _pimpl->printLineImpl();
}

//TestLine.cc
#include "Line.h"
#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char **argv)
{
    Line line(1, 2, 3, 4);
    line.printLine();
    return 0;
}

3.块作用域

类的定义在代码块中,这是所谓局部类,该类完全被块包含,其作用域仅仅限于定义所在块,不能在块外使用类名声明该类的对象。

void test() 
{    
    class Point 
    {    
    public:        
        Point(int x, int y)        
        : _x(x), _y(y)        
        {
            
        }        
        
        void print() const
        {            
            cout << "(" << _x << "," << _y << ")" << endl;        
        }    
    private:        
        int _x;        
        int _y;    
    };        
    
    Point pt(1, 2);    
    pt.print(); 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值