zoukankan      html  css  js  c++  java
  • C++中关于class B:A与Class B::A问题

    一,class B:A为类的继承关系,即A类是B类的基类
    class <派生类名>:<继承方式><基类名>
    {
    <派生类新定义成员>
    };

    例如:

    #include<iostream>
    
    class A
    {
    public:
    void print()
    { std::cout<<"A::print()"<<std::endl; }
    };
    
    class B:A //这里等价于class B:private A 即B继承A的方式为私有继承
    {
    public:
    void test()
    {
    print();
    std::cout<<"B::test()"<<std::endl; 
    }
    };
    
    int main()
    {
    
    B b;
    b.test();
    return 0;
    }

    二、Class A::B为类的嵌套关系,即A类是B类内部的类,双冒号为作用域

    如下示例为《boost程序完全开发指南》中3.4.6节中的桥接模式:

    //File:TestSample.h
    #include<boost/smart_ptr.hpp>
    
    class TestSample
    {
    private:
    class TestSampleImpl;
    boost::shared_ptr<TestSampleImpl> m_ptrImpl;
    
    public:
    TestSample();
    ~TestSample();
    
    void Display();    
    };
    
    //////////////////////////////////////////
    //File:TestSample.cpp
    #include "TestSample.h"
    #include "TestSampleImpl.h"
    #include<boost/make_shared.hpp>
    
    TestSample::TestSample()
    {
    m_ptrImpl=boost::make_shared<TestSampleImpl>();
    }
    
    TestSample::~TestSample()
    {
    
    }
    
    void TestSample::Display()
    {
    m_ptrImpl->Display();
    
    }
    
    ///////////////////////////////////////////
    //File:TestSampleImpl.h
    #include "TestSample.h"
    
    class TestSample::TestSampleImpl
    {
    public:
    TestSampleImpl(){}
    ~TestSampleImpl(){}
    
    void Display();    
    };
    
    ////////////////////////////////////////
    //File:TestSampleImpl.cpp
    #include "TestSampleImpl.h"
    #include<iostream>
    
    void TestSample::TestSampleImpl::Display()
    {
    std::cout<<"TestSampleImpl::Display()"<<std::endl;
    }
    
    
    ////////////////////////////////////////
    //File:main.cpp
    #include"TestSample.h"
    
    int main(int argc,char *argv[])
    {
    TestSample inst;
    inst.Display();
    
    return 0;
    }
  • 相关阅读:
    顺序表(线性表)操作的思想及实现之C#版
    基于CXF Java 搭建Web Service
    敏捷方法 – 灵活,可靠的软件 使用设计模式和敏捷开发
    使用HttpHanlder处理404: File not found
    关于js模块加载的尝试
    Tfs 自动部署 部署图
    jQuery能做到,PHP能做到,C#也能做到
    优化反射性能的总结(下)
    推荐系统
    10个前端开发必备的工具或使用方法
  • 原文地址:https://www.cnblogs.com/inrgihc/p/8203395.html
Copyright © 2011-2022 走看看