一,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; }