instanceof and getClass not working

Help
Bel Od
2004-07-08
2004-07-12
  • Bel Od

    Bel Od - 2004-07-08

    I still stumble on the class identity issue.  The following code
    crashes when the commented code that makes use of instanceof
    and getClass is uncommented. When commented as provided, the code produces
    the following output:

    >>>> oooo to string is aaaa
    >> oooo has a String name

    The use of ->getName()->equals() is an OK work around for me, however I
    would still like to know why the commented code does not work.
    Any hint?

    ================================

    /**
    * BasicTestRunner.h
    */

    #include <acdk.h>
    #include <acdk/lang/System.h>
    #include <acdk/util/Vector.h>

    using namespace acdk::util;

    // defining RBasicTestRunner, BasicTestRunnerArray and RBasicTestRunnerArray
    ACDK_DECL_CLASS(BasicTestRunner);

    class BasicTestRunner : public acdk::lang::Object {

       
    public:
        BasicTestRunner() : Object() {}
        static int btrmain(RStringArray args);

    };

    ===============================

    /**
    * BasicTestRunner.cpp
    */

    #include "BasicTestRunner.h"

    //static
    int BasicTestRunner::btrmain(RStringArray args) {

      RVector v = new Vector();
      RString str = new String("aaaa");
      v->addElement((RObject)str);
      RObject oooo = v->elementAt(0);
      System::out->println(">>>> oooo to string is "+oooo->toString());

      if(oooo->getName()->equals("acdk/lang/Object")) {
          System::out->println(">> oooo has an Object name");
      }
      if(oooo->getName()->equals("acdk/lang/String")) {
          System::out->println(">> oooo has a String name");
      }
      /*if(instanceof(oooo, Object)) {
        System::out->println(">> oooo is an Object instance");
      }
      if(instanceof(oooo, String)) {
        System::out->println(">> oooo is a String instance");
      }*/
      //System::out->println(">> oooo has class "+oooo->getClass());

      return 0;
    }

    int main(int argc, char* argv[], char** envptr) {
      return acdk::lang::System::main(BasicTestRunner::btrmain, argc, argv, envptr);
    }

     
    • Mika Riekkinen

      Mika Riekkinen - 2004-07-08

      Here is my output, when the code is uncommented, except for
      the oooo->getClass() call(that one remain commented):

      --< clip >--
      mcr70@snowhite:~> /opt/acdk/bin/nstest
      >>>> oooo to string is aaaa
      >> oooo has a String name
      >> oooo is an Object instance
      >> oooo is a String instance
      --< clip >--

      Now, if I uncomment getClass() call, I get interrupted...
      Don't really know why. Maybe Roger can explain this...
      Here is how you can get the correct result:
      change
      System::out->println(">> oooo has class "+oooo->getClass());
      to
      System::out->println(">> oooo has class "+oooo->getClass()->toString);
      and you will get:
      mcr70@snowhite:~> /opt/acdk/bin/nstest
      >>>> oooo to string is aaaa
      >> oooo has a String name
      >> oooo is an Object instance
      >> oooo is a String instance
      >> oooo has class class acdk.lang.String

       
    • Roger Rene Kommer

      Hi,

      I've tested it and received following output including
      the getClass() code:

      >>>> oooo to string is aaaa
      >> oooo has a String name
      >> oooo is an Object instance
      >> oooo is a String instance

      It seems to be all OK.

      Nearly nothing would work in ACDK if instanceof
      and getClass() doesn't work.

      I'm not sure what happens on your configuration, but I guess that you have to enable RTTI in our compiler. instanceof rely on dynamic_cast<T> because instanceof should also work for classes, which has no generated meta information. dynamic_cast<T> itself rely on the RTTI generated by the C++ compiler.

      For gcc you must not use the -fno-rtti flag.
      In Visual Studio you have to set the RTTI check box in the project settings (by default RTTI is off).
      In Borland C++Builder X the -RT flag has to be set.

      Hope this helps,
      Roger

       
    • Bel Od

      Bel Od - 2004-07-09

      I will try with the RTTI option on Monday when I am back to work.
      I will keep you posted.
      Thanks

       
    • Bel Od

      Bel Od - 2004-07-12

      The RTTI option fixes the problem.
      Thanks!

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks