程序基石系列之C++运行时类型转换(Runtime Cast)

通过pointer或reference来决定对象运行时类型的一种方法是使用运行时类型转换(runtime cast),用这种方法可以查证所尝试进行的转换正确与否。当要把基类pointer转换为派生类型时,这种方法非常有用。由于inheritance的层次结构的典型描述是基类在派生类之上,所以这种类型转换也称为向下类型转换(downcast).

在下面的程序代码中,Investment类有一个其他类没有的额外操作,所以能够在运行时知道Security指针是否引用了Investment对象是很重要的。为了实现检查运行时的类型转换,每个类都持有一个整数标识符,以便可以与层次结构中中其他的类区别开来。

purge.h代码:

/*
 *  purge.h
 *
 *  Created on: Feb 22, 2014
 *  Author: Gavin Liu
 */

#ifndef PURGE_H_
#define PURGE_H_

#include <algorithm>

template<class Seq> void purge(Seq& c) {
  typename Seq::iterator i;
  for(i = c.begin(); i != c.end(); ++i) {
    delete *i;
    *i = 0;
  }
}

// Iterator version:
template<class InpIt> void purge(InpIt begin, InpIt end) {
  while(begin != end) {
    delete *begin;
    *begin = 0;
    ++begin;
  }
}

#endif /* PURGE_H_ */
CheckedCast.cpp代码:

/*  Thinking in C ++ Vol. two Practical Programming
 *  CheckedCast.cpp
 *  Checks cast runtime
 *  Created on: Feb 22, 2014
 *  Author: Gavin Liu
 *  Eclipse CDT and GCC
 */

#include <iostream>
#include <vector>
#include "purge.h"
using namespace std;

class Security {
protected:
  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<Stock*>(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<Bond*>(s) : 0;
  }
};

class Investment : public Security {
  typedef Security Super;
protected:
  enum { OFFSET = 3, TYPEID = BASEID + OFFSET };
public:
  bool isA(int id) {
    return id == TYPEID || Super::isA(id);
  }
  static Investment* dynacast(Security* s) {
    return (s->isA(TYPEID)) ?
      static_cast<Investment*>(s) : 0;
  }
  void special() {
    cout << "special Investment function" << endl;
  }
};

class Metal : public Investment {
  typedef Investment Super;
protected:
  enum { OFFSET = 4, TYPEID = BASEID + OFFSET };
public:
  bool isA(int id) {
    return id == TYPEID || Super::isA(id);
  }
  static Metal* dynacast(Security* s) {
    return (s->isA(TYPEID)) ? static_cast<Metal*>(s) : 0;
  }
};

int main() {
  vector<Security*> portfolio;
  
  portfolio.push_back(new Metal);
  portfolio.push_back(new Investment);
  portfolio.push_back(new Bond);
  portfolio.push_back(new Stock);
  
  for(vector<Security*>::iterator it = portfolio.begin();
       it != portfolio.end(); ++it) {
    Investment* cm = Investment::dynacast(*it);
    if(cm)
      cm->special();
    else
      cout << "not an Investment" << endl;
  }
  cout << "cast from intermediate pointer:" << endl;
  Security* sp = new Metal;
  Investment* cp = Investment::dynacast(sp);
  if(cp) 
      cout << "  it's an Investment" << endl;
  Metal* mp = Metal::dynacast(sp);
  if(mp) 
      cout << "  it's a Metal too!" << endl;
  purge(portfolio);
}

其中多态isA()函数检查其参数是否与它的类型参数相容,就意味或id与对象的typeID准确地匹配。


关于程序设计基石与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.


你可能感兴趣的:(运行时类型转换,程序基石)