Читаем Thinking In C++. Volume 2: Practical Programming полностью

  enum {BASEID = 0};

public:

  virtual ~Security() {}

  virtual bool isA(int id) {

     return (id == BASEID);

  }

};

class Stock : public Security {

  typedef Security Super;

protected:

  enum {OFFSET = 1, TYPEID = BASEID + OFFSET};

public:

  bool isA(int id) {

    return id == TYPEID || Super::isA(id);

  }

  static Stock* dynacast(Security* s) {

    return (s->isA(TYPEID)) ?

      static_cast(s) : 0;

  }

};

class Bond : public Security {

  typedef Security Super;

protected:

  enum {OFFSET = 2, TYPEID = BASEID + OFFSET};

public:

  bool isA(int id) {

    return id == TYPEID || Super::isA(id);

  }

  static Bond* dynacast(Security* s) {

    return (s->isA(TYPEID)) ?

      static_cast(s) : 0;

  }

};

class Investment : public Security {

  typedef Security Super;

protected:

  enum {OFFSET = 3, TYPEID = BASEID + OFFSET};

public:

  bool isA(int id) {

    return id == BASEID || Super::isA(id);

  }

  static Investment* dynacast(Security* s) {

    return (s->isA(TYPEID)) ?

      static_cast(s) : 0;

  }

  void special() {

    cout << "special Investment function\n";

  }

};

class Metal : public Investment {

  typedef Investment Super;

protected:

  enum {OFFSET = 4, TYPEID = BASEID + OFFSET};

public:

  bool isA(int id) {

    return id == BASEID || Super::isA(id);

  }

  static Metal* dynacast(Security* s) {

    return (s->isA(TYPEID)) ?

      static_cast(s) : 0;

  }

};

int main() {

  vector portfolio;

  portfolio.push_back(new Metal);

  portfolio.push_back(new Investment);

  portfolio.push_back(new Bond);

  portfolio.push_back(new Stock);

  for (vector::iterator it =

         portfolio.begin();

       it != portfolio.end(); ++it) {

    Investment* cm = Investment::dynacast(*it);

    if(cm)

      cm->special();

    else

      cout << "not a Investment" << endl;

  }

  cout << "cast from intermediate pointer:\n";

  Security* sp = new Metal;

  Investment* cp = Investment::dynacast(sp);

  if(cp) cout << "  it's an Investment\n";

  Metal* mp = Metal::dynacast(sp);

  if(mp) cout << "  it's a Metal too!\n";

  purge(portfolio);

} ///:~

The polymorphic isA( ) function checks to see if its argument is compatible with its type argument (id), which means that either id matches the object’s typeID exactly or that of one of its ancestors in the hierarchy (hence the call to Super::isA( ) in that case). The dynacast( ) function, which is static in each class, calls isA( ) for its pointer argument to check if the cast is valid. If isA( ) returns true, the cast is valid, and a suitably cast pointer is returned. Otherwise, the null pointer is returned, which tells the caller that the cast is not valid, meaning that the original pointer is not pointing to an object compatible with (convertible to) the desired type. All this machinery is necessary to be able to check intermediate casts, such as from a Security pointer that refers to a Metal object to a Investment pointer in the previous example program.[104] 

Although for most programs downcasting is not needed (and indeed is discouraged, since everyday polymorphism solves most problems in object-oriented application programs), the ability to check a cast to a more derived type is important for utility programs such as debuggers, class browsers, and databases. C++ provides such a checked cast with the dynamic_cast operator. The following program is a rewrite of the previous example using dynamic_cast.

//: C08:CheckedCast2.cpp

// Uses RTTI’s dynamic_cast

#include

#include

#include "../purge.h"

using namespace std;

class Security {

public:

  virtual ~Security(){}

};

class Stock : public Security {};

class Bond : public Security {};

class Investment : public Security {

public:

  void special() {

    cout << "special Investment function\n";

  }

};

class Metal : public Investment {};

int main() {

  vector portfolio;

  portfolio.push_back(new Metal);

  portfolio.push_back(new Investment);

  portfolio.push_back(new Bond);

  portfolio.push_back(new Stock);

  for (vector::iterator it =

                    portfolio.begin();

       it != portfolio.end(); ++it) {

    Investment* cm = dynamic_cast(*it);

    if(cm)

      cm->special();

    else

      cout << "not a Investment" << endl;

  }

  cout << "cast from intermediate pointer:\n";

  Security* sp = new Metal;

  Investment* cp = dynamic_cast(sp);

  if(cp) cout << "  it's an Investment\n";

  Metal* mp = dynamic_cast(sp);

  if(mp) cout << "  it's a Metal too!\n";

  purge(portfolio);

} ///:~

Перейти на страницу:

Похожие книги

3ds Max 2008
3ds Max 2008

Одни уверены, что нет лучшего способа обучения 3ds Мах, чем прочитать хорошую книгу. Другие склоняются к тому, что эффективнее учиться у преподавателя, который показывает, что и как нужно делать. Данное издание объединяет оба подхода. Его цель – сделать освоение 3ds Мах 2008 максимально быстрым и результативным. Часто после изучения книги у читателя возникают вопросы, почему не получился тот или иной пример. Видеокурс – это гарантия, что такие вопросы не возникнут: ведь автор не только рассказывает, но и показывает, как нужно работать в 3ds Мах.В отличие от большинства интерактивных курсов, где работа в 3ds Мах иллюстрируется на кубиках-шариках, данный видеокурс полностью практический. Все приемы работы с инструментами 3ds Мах 2008 показаны на конкретных примерах, благодаря чему после просмотра курса читатель сможет самостоятельно выполнять даже сложные проекты.

Владимир Антонович Верстак , Владимир Верстак

Программирование, программы, базы данных / Программное обеспечение / Книги по IT