第六章 类域
一、类作用域
使用::
访问全局变量
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
PIMPL
(Private Implementation
或 Pointer to Implementation
)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。PIMPL
又称作“编译防火墙”,它的实现中就用到了嵌套类。PIMPL
设计模式有如下优点:
- 提高编译速度;
- 实现信息隐藏;
- 减小编译依赖,可以用最小的代价平滑的升级库文件;
- 接口与实现进行解耦;
- 移动语义友好
//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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)