c++ getClass()?

Discussion in 'C++' started by cmk128@hotmail.com, Nov 15, 2006.

  1. Guest

    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 ()
    , Nov 15, 2006
    #1
    1. Advertising

  2. Noah Roberts Guest

    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.
    Noah Roberts, Nov 15, 2006
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Nov 15, 2006
    #3
  4. Daniel T. Guest

    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.
    Daniel T., Nov 15, 2006
    #4
  5. Guest

    Daniel T. ¼g¹D¡G

    > 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 :)
    , Nov 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. klauern
    Replies:
    9
    Views:
    7,017
    Steve W. Jackson
    Apr 12, 2005
  2. Boki
    Replies:
    34
    Views:
    27,888
  3. Replies:
    6
    Views:
    9,340
    Viator
    Nov 24, 2005
  4. JavaEnquirer

    Generics and getClass

    JavaEnquirer, Feb 10, 2006, in forum: Java
    Replies:
    4
    Views:
    24,523
    darkaico
    May 4, 2011
  5. Knute Johnson
    Replies:
    5
    Views:
    33,364
    Knute Johnson
    Mar 31, 2006
Loading...

Share This Page