⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 innerclassidiom.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C25:InnerClassIdiom.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Example of the "inner class" idiom

#include <iostream>

#include <string>

using namespace std;



class Poingable {

public:

  virtual void poing() = 0;

};



void callPoing(Poingable& p) {

  p.poing();

}



class Bingable {

public:

  virtual void bing() = 0;

};



void callBing(Bingable& b) {

  b.bing();

}



class Outer {

  string name;

  // Define one inner class:

  class Inner1;

  friend class Outer::Inner1;

  class Inner1 : public Poingable {

    Outer* parent;

  public:

    Inner1(Outer* p) : parent(p) {}

    void poing() {

      cout << "poing called for "

        << parent->name << endl;

      // Accesses data in the outer class object

    }

  } inner1;

  // Define a second inner class:

  class Inner2;

  friend class Outer::Inner2;

  class Inner2 : public Bingable {

    Outer* parent;

  public:

    Inner2(Outer* p) : parent(p) {}

    void bing() {

      cout << "bing called for "

        << parent->name << endl;

    }

  } inner2;

public:

  Outer(const string& nm) : name(nm), 

    inner1(this), inner2(this) {}

  // Return reference to interfaces

  //  implemented by the inner classes:

  operator Poingable&() { return inner1; }

  operator Bingable&() { return inner2; }

};



int main() {

  Outer x("Ping Pong");

  // Like upcasting to multiple base types!:

  callPoing(x);

  callBing(x);

} ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -