c++ getClass()?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmk128@hotmail.com

    #1

    c++ getClass()?

    Hi
    How can i do something like Java:

    if (str.getClass() ==String.class) {
    }


    i found that, this syntax has error in g++:

    if (typeof(a) == typeof(b)){
    }

    thanks
    from Peter (cmk128@hotmail .com)

  • Noah Roberts

    #2
    Re: c++ getClass()?


    cmk...@hotmail. com wrote:
    Hi
    How can i do something like Java:
    >
    if (str.getClass() ==String.class) {
    }
    >
    >
    i found that, this syntax has error in g++:
    >
    if (typeof(a) == typeof(b)){
    }
    try typeid.

    Comment

    • Victor Bazarov

      #3
      Re: c++ getClass()?

      cmk128@hotmail. com wrote:
      Hi
      How can i do something like Java:
      >
      if (str.getClass() ==String.class) {
      }
      >
      >
      i found that, this syntax has error in g++:
      >
      if (typeof(a) == typeof(b)){
      }
      Try

      if (typeid(a) == typeid(b)){

      Don't forget to '#include <typeinfo>'

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • Daniel T.

        #4
        Re: c++ getClass()?

        cmk128@hotmail. com wrote:
        Hi
        How can i do something like Java:
        >
        if (str.getClass() ==String.class) {
        }
        >
        >
        i found that, this syntax has error in g++:
        >
        if (typeof(a) == typeof(b)){
        }
        class A { public: virtual ~A() { } };
        class B : public A { };

        int main()
        {
        A a, b;
        assert( typeid( a ) == typeid( b ) );
        B c;
        assert( typeid( a ) != typeid( c ) );
        }

        Frankly though, if you need to do the above, then you probably have a design problem.

        --
        To send me email, put "sheltie" in the subject.

        Comment

        • cmk128@hotmail.com

          #5
          Re: c++ getClass()?


          Daniel T. ¼g¹D¡G
          cmk128@hotmail. com wrote:
          >
          Hi
          How can i do something like Java:

          if (str.getClass() ==String.class) {
          }


          i found that, this syntax has error in g++:

          if (typeof(a) == typeof(b)){
          }
          >
          class A { public: virtual ~A() { } };
          class B : public A { };
          >
          int main()
          {
          A a, b;
          assert( typeid( a ) == typeid( b ) );
          B c;
          assert( typeid( a ) != typeid( c ) );
          }
          >
          Frankly though, if you need to do the above, then you probably have a design problem.
          >
          --
          To send me email, put "sheltie" in the subject.
          thank you so much :)

          Comment

          Working...