第六章 类域

2023-05-16

第六章 类域

一、类作用域

使用::访问全局变量

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(); 
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

第六章 类域 的相关文章

  • DDR协议解析

    DRAM内部分割成多个L Bank xff0c 每个L Bank形状相同 xff0c 彼此独立 xff0c 可以独立工作 早期的DRAM芯片内部分为2个L Bank xff0c 后来是4个 xff0c DDR3内存芯片为8个 在进行寻址时需
  • apt-get安装指定版本&查询版本

    一 通过apt get安装指定版本 apt get install lt lt package name gt gt 61 lt lt version gt gt 二 查询指定软件有多少个版本 说明 xff1a 在Linux用这个查询并不能
  • 使用apt-get install时有时候一堆依赖要安装,一个一个安装特别烦人,可以直接用suggest全部安装,具体命令如下

    使用apt get install时有时候一堆依赖要安装 xff0c 一个一个安装特别烦人 xff0c 可以直接用suggest全部安装 xff0c 具体命令如下 apt get install install suggests packa
  • linux-Centos 7下tftp-server服务的安装与配置

    转自 http www cnblogs com 5201351 p 4934625 html TFTP xff08 Trivial File Transfer Protocol 简单文件传输协议 xff09 是TCP IP协议族中的一个用来
  • Linux启动打印信息

    U Boot 1 1 6 Oct 5 2016 16 45 02 for SMDK6410 u boot 1 1 6 Updated for OK6410 TE6410 Board Version 2012 09 23 OEM Forlin
  • 对比S3C6410外部中断STM32外部中断

    转自 xff1a http comm chinaaet com adi blogdetail aspx id 61 40071 amp currentpage 61 2 a S3C6410外部中断 中断在嵌入式里面是很常见的一个功能了 通过
  • shell脚本记录

    1 find name o 找出当前目录下所有的 o文件 使用在makefile中如下 clean rm f liblog so 96 find name o 96
  • makefile中的patsubst

    1 wildcard 扩展通配符 2 notdir xff1a 去除路径 3 patsubst xff1a 替换通配符 例子 xff1a 建立一个测试目录 xff0c 在测试目录下建立一个名为sub的子目录 mkdir test cd te
  • ElasticSearch学习&&理解

    注 xff1a 本篇的es基于7 5 1版本 目录 Elasticsearch是什么 xff1f ElasticSearch的环境搭建 ElasticSearch的名词 ElasticSearch查询出的数据格式 ElasticSearch
  • Kibana学习&理解

    注 xff1a 本篇的kibana基于7 5 1版本 Kibana是什么 xff1f kibana是一个数据可视化平台 展示与分析 将es里面的东西通过各种图表展示出来 xff0c 还可以执行es的各种搜索 amp 监控 Kibana环境搭
  • filebeat学习

    注 xff1a 本篇基于filebeat7 5 2 filebeat是什么 xff1f Filebeat 是用于转发和集中日志数据的轻量级传送程序 作为服务器上的代理安装 xff0c Filebeat 监视您指定的日志文件或位置 xff0c
  • Git Flow 用法

    git flow 工作流程 如下图所示 master 分支 master 分支主要方稳定 随时可上线的版本 这个分支只能从别的分支上合并过来 xff0c 一般来讲 xff0c 从develop 上合并 xff0c 或者从hotfix分支上合
  • Qt父窗口与子窗口间的焦点传递问题的完美解决

    使用activateWindow 或者raise 参考文章 xff1a https blog csdn net Hoarce article details 107215868 http www manongjc com detail 19
  • Git 工作中的一些命令操作

    本篇为工作中 git 使用过程中的一些操作记载 xff0c 不定期更新 目录 1 git 推本地代码到远程 2 git 放弃修改 commit 撤销远程提交记录 3 git pull push fetch 4 git关联本地与远程分支 5
  • php如何使用S3

    本篇是新手使用PHP调aws的s3服务的一些心得 一 关于AWS S3 s3是一个文件存储服务 xff0c 当需要做成服务来进行微服务调用 xff0c 或者终端服务端文件交流使用s3是一个非常不错的选择 aws各种常见的语言例如 xff1a
  • MySQL相关面试题

    1 MySQL text长度 mysql的text是65535的字节限制 xff0c 而pg是不限制的 2 覆盖索引 聚簇索引 xff08 https blog csdn net alexdamiao article details 519
  • Redis相关面试题

    1 缓存是什么 xff1f 缓存分为本地缓存和分布式缓存 以Java为例 xff0c guava实现的就是本地缓存 xff0c 生命周期随JVM销毁而结束 起多个服务实例 xff0c 就有多份缓存 xff0c 不具有一致性 redis和me
  • 如何断网安装docker

    docker rpm安装 不能联网情况 生产环境可能是不能联网的 xff0c 当我们需要用到docker 或其他组件 的时候 xff0c 就需要借助能联网的环境下载好rpm包 xff0c 然后去操作系统服务器装下载好的docker RPM包
  • docker相关

    优势 xff1a 1 启动快 传统的虚拟机技术启动应用服务往往需要数分钟 xff0c 而 Docker 容器应用 xff0c 由于直接运行于宿主内核 xff0c 无需启动完整的操作系统 xff0c 因此可以做到秒级 甚至毫秒级的启动时间 大
  • Liunx下源代码安装&&make&&makefile

    Linux下安装软件的方式分为源代码安装和二进制安装 源代码安装 xff0c 即使用应用程序源代码进行编译安装二进制安装 xff0c 例如red hat发行的 rpm包 debian发行的 deb包 源代码安装 用c语言为例 include

随机推荐

  • Linux下rpm&yum&apt-get

    RPM简介 RPM命名 RedHat Package Manager xff0c 简称则为RPM 属于Red Hat阵营的 xff0c 与其并列的则是debian centos中大部分我们安装都是使用yum install xff0c 而d
  • Java面试题复习整理(多线程)

    文章目录 1 aio nio bio epoll select2 reactor模式介绍Reactor软件工程java代码总结 3 Java中的cas乐观锁4 自旋锁是什么 xff1f 自旋锁 amp amp 自适应自旋锁 amp amp
  • Grpc&&protocol buffer结合提供grpc服务

    Grpc amp amp protocol buffer 关于下载 xff1a 首先下载一个protobuf 对于mac系统就brew install protobuf 就可以了 然后可以 protoc version 看下安装的版本号 x
  • 指针p,*p,&p之间的区别

    假设我们定义一个指针p 那么会经常使用到三个符号 xff1a 1 xff0c p xff1b p是一个指针变量的名字 xff0c 表示此指针变量指向的内存地址 xff0c 如果使用 p来输出的话 xff0c 它将是一个16进制数 2 xff
  • 自己经历的Java面试题(附答案)

    本篇记录一下自己面试的一些中大厂的 1 3年Java开发的面试题以及自己对题目理解的答案 xff08 结合网上查的资料 xff09 部分可能更新的不及时 xff0c 有问题可以评论区讨论 面试题不分先后 1 hashmap rehash 过
  • Redisson相关使用&&分布式锁浅析

    注 xff1a 本篇的redisson版本基于3 13 3 xff1b 本篇的demo将我写的源代码贴了出来 xff0c 每个方法都有清晰的注释 xff0c 分布式锁相关的代码以及验证是我手动验证Redis中key状态来判断的 文章目录 简
  • SpringBoot实战elasticSearchAPI微服务开发

    文章目录 前言启动一个ElasticSearchSpringBoot引入ElasticSearch索引创建 amp amp 更新插入删除 写 操作ElasticSearch的API查询操作源码参考 前言 本文准备记录一下ElasticSea
  • Leetcode链表刷题思路汇总

    链表 链表相关的题 xff0c 最快的入门方法就是做题的时候画图 标注A的next节点是哪里 xff0c 单链表的遍历只能是单向的 xff0c 从头结点到尾结点 例如 xff1a 给你一个链表的head头 xff0c 让你返回最后元素的值
  • Java基础之字符串&equals、==、包装类、常量池

    在java中有三中对字符串的操作方式 注 xff1a 文章只注明思路原理 不注明方法 xff0c 看API就行了 文章就涉及到啥写啥了 xff0c 哈哈 xff0c 瞅着可能乱一点 但是这么写就很舒服 1 String 常量 效率较低 指的
  • 基于激光雷达的人型识别(无人车)

    原文在这 finalobject cn 这个项目是无人车里的一部分 xff0c 完成激光雷达的驱动 xff0c 数据采集然后后期的处理以及人型识别 xff0c 并不涉及车辆硬件的控制 主要分三个大块讲吧 xff0c 硬件驱动 数据聚类 xf
  • ARM Cortex M4 SVC指令作用

    xff08 1 xff09 SVC指令 xff1a 摘自 http infocenter arm com help index jsp topic 61 com arm doc dui0203ic Cacdfeci html 与更早版本的
  • SVC和PendSV

    转载于 xff1a http book 2cto com 201209 4625 html 1 xff0e SVC SVC xff08 Supervisor Call xff09 指令用于产生一个SVC异常 它是用户模式代码中的主进程 xf
  • esp8266烧录Html文件,实现内置网页控制设备!

    代码地址如下 xff1a http www demodashi com demo 14321 html 一 前言 xff1b 这个月也快结束了 xff0c 时间真快 xff0c 我服务器知识自学依然在路途中 xff0c 这几天听到热点网页配
  • Golang创建XML

    package main import 34 encoding xml 34 34 fmt 34 34 io ioutil 34 type Post struct XMLName xml Name 96 xml 34 post 34 96
  • xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    1 解压 tar zxvf wmsdk bundle xxxxxxx tar gz cd wmsdk bundle xxxxxxx 2 make installpkgs 或者installpkgs sh 3 安装gcc arm none e
  • 超声波换能器的几点总结

    超声波换能器是超声波测量的关键件 xff0c 必须保证超声波换能器的质量以及特性稳定 1 超声波换能器的激励信号频率接近谐振频率是触发信号强度最大 2 传感器是被动元器件 xff0c 激励信号进行激励会产生震动 xff0c 接收端接收信号
  • windows7安装ubuntu双系统教程

    转载自 xff1a http www cnblogs com masbay p 10745170 html windows7安装ubuntu双系统教程 一 先搞清楚自己电脑的类型 xff1a 本次安装的Ubuntu的电脑类型是MBR传统bi
  • STM32 串口详解

    目录 01 USART的特点 02 USART简介 2 1 数据传输模型 2 2 帧结构 2 3 波特率 03 STM32的USART 04 代码配置 01 USART的特点 USART是通用异步收发传输器 xff08 UniversalA
  • STM32串口开发之环形缓冲区

    01 简介 在之前的文章 stm32 串口详解 中 xff0c 我们讲解了串口的基本应用 xff0c 使用串口中断接收数据 xff0c 串口中断发送回包 xff08 一般可以使用非中断形式发送回包 xff0c 在数据接收不频繁的应用中 串口
  • 第六章 类域

    第六章 类域 一 类作用域 使用 访问全局变量 int x 61 1 namespace wd int x 61 20 class Test public Test int value x value void print int x co