🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #4029

Is RTTI required if polymorphic constructor exists

#4029
From: Satrajit Sujit Ghosh
Date: Sun, 14 Apr 1996 12:32
156 lines
3797 bytes
Hi,
	If a polymorphic constructor can be created as demonstrated below, 
is RTTI really necessary, considering that it does not really provide a 
great deal of information.

--
Satra[jit]
------------------------------------------------------
http://www.iscs.nus.sg/~satrajit
Department of Information Systems and Computer Science
National University of Singapore
======================================================
/*
/////////////////THE POLYMORPHIC CONSTRUCTOR///////////////////
The following code segments demonstrate the creation of a polymorphic 
constructor. 
///////////////////////////////////////////////////////////////
*/

#include <iostream.h>
#include <string.h>

template<class T>
class Clist{
public:
  Clist(){
    i = 0;
  };

  void insert(char *name,T *(*func)()){
    for(int j=0;j<i;j++)
      // prevent re-registration
      if (!strcmp(nm_arr[j],name)){
	return;
      }

    nm_arr[i] = strdup(name);
    fun[i] = func;
    i++;
  }

  T* get(char *name){
    int j;
    for(j=0;j<i;j++)
      if (!strcmp(nm_arr[j],name)){
	return (*fun[j])();
      }
    return NULL;
  };

  // currently can support 5 derived classes
  char *nm_arr[5];
  T *(* fun[5])();
  int i;
};



class CShape{
public:
  virtual void print(){
    cout << "reg" << endl;
  };

  static CShape * mk_subclass(char *str){
    return reg.get(str);
  }

protected:
  static int reg_sub(char *name,CShape *(*func)()){
    reg.insert(name,func);
    return 0;
  };

private:  
  static Clist<CShape> reg;

};

Clist<CShape> CShape::reg;

class CCircle:public virtual CShape{
public:
  virtual void print(){
    cout <<"CCircle";
  };

  static CShape* int_new(){
    CShape *p = new CCircle;
    return p;
  };
private:
  static int i;
};

int CCircle::i = reg_sub("CCircle",&CCircle::int_new);

class CRect:public virtual CShape{
public:

  virtual void print(){
    cout <<"CRect";
  };

  static CShape* int_new(){
    CShape *p = new CRect;
    return p;
  };

private:
  static int i;
};

int CRect::i =  reg_sub("CRect",&CRect::int_new);

class CSquare:public virtual CRect{
public:

  virtual void print(){
    cout <<"CSquare";
  };

  static CShape* int_new(){
    CShape *p = new CSquare;
    return p;
  };

private:
  static int i;
};

int CSquare::i =  reg_sub("CSquare",&CSquare::int_new);


int 
main(){
  CShape *fg = CShape::mk_subclass("CCircle");
  cout << "Testing constructed CCircle [";
  fg->print();
  cout << "]\n";
  CShape *fg1 = CShape::mk_subclass("CRect");
  cout << "Testing constructed CRect [";
  fg1->print();
  cout << "]\n";
  CShape *fg2 = CShape::mk_subclass("CSquare");
  cout << "Testing constructed CSquare [";
  fg2->print();
  cout << "]\n";
  return 0;
}
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <4kng7f$kt9@nuscc.nus.sg>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back

Show Full Headers
Complete Article Headers
From: satrajit@iscs.nus.sg (Satrajit Sujit Ghosh)
Message-ID: <4kng7f$kt9@nuscc.nus.sg>
X-Original-Date: 13 Apr 1996 06:05:35 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 14 Apr 96 12:32:30 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Is RTTI required if polymorphic constructor exists
Organization: National University of Singapore
X-Newsreader: TIN [version 1.2 PL2]
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMXDwaeEDnX0m9pzZAQFnnwF/daFCCOOaQUZxmoMjg40Z/7l0XrhjH8Z9
 OkT3/cQb61GsmtOS0dVO7lPnixGAb9qi
 =tvUc