2011年3月24日——学习笔记

来源:互联网 发布:jupiter python 编辑:程序博客网 时间:2024/06/06 06:48

1、容器和容器适配器有什么区别和联系,为什么容器适配器模板要提供两个类型,这两个类型各代表什么?

容器vector,list,deuqe是C++STL中三种基本容器实现,它们不可能互为实现同时又不损失效率, 就像颜色中的三原色红绿蓝可以混成其他多种颜色.
而stack和queue则都可以在这三种基本容器序列基础上高效实现, 所以没有定义为独立的容器,而只作为基本容器适配器.所以容器适配器所提供的是原来容器的一个受限的界面, 特别是适配器不提供迭代器. 所有stack和queue是用deque基本容器作为实现方式的.容器适配器模板要提供两个类型1是容器中元素的类型, 2是选择的实现方式
2、友元、static与继承1、友元关系不能继承:基类的友元对派生类的成员没有特殊访问权限。如果基类被授予友元关系,则只有基类具有特殊访问权限,该基类的派生类不能访问授予友元关系的类。每个类控制对自己的成员的友元关系:     程序代码:class Base {         friend class Frnd;     protected:         int i;     };     // Frnd has no access to members in D1     class D1 : public Base {     protected:         int j;     };     class Frnd {     public:        int mem(Base b) { return b.i; }  // ok: Frnd is friend to Base        int mem(D1 d) { return d.i; }    // error: friendship doesn't inherit     };     // D2 has no access to members in Base     class D2 : public Frnd {     public:        int mem(Base b) { return b.i; } // error: friendship doesn't inherit     };如果派生类想要将自己成员的访问权授予其基类的友元,派生类必须显式地这样做:基类的友元对从该基类派生的类型没有特殊访问权限。同样,如果基类和派生类都需要访问另一个类,那个类必须特地将访问权限授予基类的和每一个派生类。2、继承与静态成员如果基类定义 static 成员(第 12.6 节),则整个继承层次中只有一个这样的成员。无论从基类派生出多少个派生类,每个 static 成员只有一个实例。static 成员遵循常规访问控制:如果成员在基类中为 private,则派生类不能访问它。假定可以访问成员,则既可以通过基类访问 static 成员,也可以通过派生类访问 static 成员。一般而言,既可以使用作用域操作符也可以使用点或箭头成员访问操作符。程序代码:[color=#0000FF]struct Base {         static void statmem(); // public by default     };     struct Derived : Base {         void f(const Derived&);     };     void Derived::f(const Derived &derived_obj)     {        Base::statmem();      // ok: Base defines statmem        Derived::statmem();   // ok: Derived in herits statmem        // ok: derived objects can be used to access static from base        derived_obj.statmem();     // accessed through Derived object        statmem();                 // accessed through this class    }