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;
    }
  • 相关阅读:
    NoSQL数据库 continue posting...
    CAP 理论
    Clojure Web 开发 (一)
    HttpClient 4.0.x Tips
    zZ Java中String和Byte[]之间的那些事
    使用nhibernate出现Could not find the dialect in the configuration
    eclipse导入项目出现Project has no default.properties file! Edit the project properties to set one.
    今天开通此博~
    美国白蛾入侵北京 GIS兵法破解危局
    HTML5 存取Json
  • 原文地址:https://www.cnblogs.com/inrgihc/p/8203395.html
Copyright © 2011-2022 走看看