Topic: run time type id with new


Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/12/13
Raw View
Bryan P Walsh wrote:
>
> The following program will compile and run using g++, but won't coompile
> with SGI's 7.3 compiler:

[ code using "typeof" ]

"typeof" is not part of standard C++. It is a language extension
provided by few compilers. (I don't know of any besides gnu.)

--
Steve Clamage, stephen.clamage@sun.com


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Bryan P Walsh <bryanw@earthlink.net>
Date: 1999/12/11
Raw View
The following program will compile and run using g++, but won't coompile
with SGI's 7.3 compiler:

#include <iostream.h>
#include <stdlib.h>

class base {
  public:
     base() {};
    ~base() {};
     virtual int isA() {return 1;}
};
class sub : public base  {
   public:
     sub() {};
     sub() {};
     int isA() {return 2;}
};

int main(int argc, char *argv[])
{
  sub i;
  base *test = new typeof(i);
  cerr << " i is "<<i.isA()<<" test is"<<test->isA()<<endl;

}

output:
 i is 2 test is 2

When I first ran this with g++ I was suprised that it worked at all.  I
don't believe typeof is even part of the C++ standard but I would also
think that you could do something similar to this with RTTI.  Is there a
correct or a standard way to do something like this?

Thanks,

Bryan Walsh
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]