RTTI

Discussion in 'C++' started by gouqizi.lvcha@gmail.com, Mar 14, 2005.

  1. Guest

    Hey all,

    Does standard C++ support RTTI? For example, I would like to
    dynamically
    get a class's name.

    Rick
     
    , Mar 14, 2005
    #1
    1. Advertising

  2. John Carson Guest

    <> wrote in message
    news:
    > Hey all,
    >
    > Does standard C++ support RTTI? For example, I would like to
    > dynamically
    > get a class's name.
    >
    > Rick


    Yes.

    --
    John Carson
     
    John Carson, Mar 14, 2005
    #2
    1. Advertising

  3. "" wrote:
    >
    > Hey all,
    >
    > Does standard C++ support RTTI? For example, I would like to
    > dynamically
    > get a class's name.
    >


    look up functions
    typeid()
    typename()
    and the structure 'type_info'

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 14, 2005
    #3
  4. Rolf Magnus Guest

    wrote:

    > Hey all,
    >
    > Does standard C++ support RTTI? For example, I would like to
    > dynamically get a class's name.


    Yes. Try typeid(MyClass).name()
    However, the name that you get back from this is implementation defined. It
    might not be the same as it is defined in your source code.
     
    Rolf Magnus, Mar 14, 2005
    #4
  5. Guest

    Hi Rolf,

    I tried. gcc 3.2 does not give the same as my source code as you said
    it.
    What can I do if I want to get the exactly the same name as I defined
    in code?

    Rick
     
    , Mar 14, 2005
    #5
  6. red floyd Guest

    wrote:
    > Hi Rolf,
    >
    > I tried. gcc 3.2 does not give the same as my source code as you said
    > it.
    > What can I do if I want to get the exactly the same name as I defined
    > in code?
    >
    > Rick
    >


    create a member function "const char * get_class_name();"
     
    red floyd, Mar 14, 2005
    #6
  7. Guest

    Why isn't this standardized? Implementation difficulties? Run-time
    inefficiencies?

    I'm sorta thinking that having the RTTI work properly for derived
    classes would require all classes to have vtables, is this why?
     
    , Mar 14, 2005
    #7
  8. Guest

    Have to do like this way.

    Do you know why gcc do in such way?
     
    , Mar 14, 2005
    #8
  9. Greg Comeau Guest

    In article <>,
    <> wrote:
    >I tried. gcc 3.2 does not give the same as my source code as you said
    >it.
    >What can I do if I want to get the exactly the same name as I defined
    >in code?


    If I understand what you're asking, you don't, you get whatever
    the _vendor_ chooses for it.
    --
    Greg Comeau / Comeau for the Mac? Stay tuned.
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Mar 14, 2005
    #9
  10. Ron Natalie Guest

    wrote:
    > Hey all,
    >
    > Does standard C++ support RTTI?


    Yes.
    > I would like to
    > dynamically
    > get a class's name.
    >

    It however does NOT support that.
    You'll have to write your own virtual method to return the class name.
     
    Ron Natalie, Mar 15, 2005
    #10
  11. Ron Natalie Guest

    wrote:
    > Why isn't this standardized? Implementation difficulties? Run-time
    > inefficiencies?

    Because it's not generally useful. Identifiers in general are compile
    time issues. They don't exist in the executables.
     
    Ron Natalie, Mar 15, 2005
    #11
  12. Guest

    I think it is quite useful in Polymorphism. For example, Class A has
    two subclasses B and C.You have another function with return type as
    class A, but the return value maybe an object of class B or class C. So
    you have to determine at the run time the return value is actually of
    class B or class C . Besides RTTI, how can you achieve this?
     
    , Mar 16, 2005
    #12
  13. Seanairt Guest

    On Tue, 15 Mar 2005 18:28:10 -0800, wrote:

    > I think it is quite useful in Polymorphism. For example, Class A has
    > two subclasses B and C.You have another function with return type as
    > class A, but the return value maybe an object of class B or class C. So
    > you have to determine at the run time the return value is actually of
    > class B or class C . Besides RTTI, how can you achieve this?


    Other than class loading based on name, I really don't believe it is all
    that useful. One thing you could do is use the type_info's == or !=
    operators to check against known types in order to choose an algorithm. No
    need for the class's name.

    Or you can use compile-time polymorphism. Assuming
    your going to use the type to dynamically choose an algorithm, you could
    use templates & template specialization. You'd might
    provide a generic behavior for all type other than B or C (in your
    example) and specialize on B and/or C.

    Conversely, if you want ONLY B & C to be handled you can provide a declaration
    w/o an implementation for the most generic case and provide an implementation
    for the specializations of B & C. This way you would get a compile-time error
    if you attempted to choose your algorithm for any case other B & C.

    I haven't even touched on using reinterpret_cast, dynamic_cast and their
    like. I think if you dig a little deeper, you'll find whatever your
    designing will be a lot better off (certainly nowhere near as brittle)
    than if you base your design on a bunch of hard-coded static strings.
    Consider, for example, what happens when you further subclass B or C? A
    B_derived class is still a B, but will not, certainly, return "B" when you
    call name(). I think you can see where this leads. I'll leave you to it.
     
    Seanairt, Mar 16, 2005
    #13
  14. In message <>,
    "" <> writes
    >I think it is quite useful in Polymorphism. For example, Class A has
    >two subclasses B and C.You have another function with return type as
    >class A, but the return value maybe an object of class B or class C.


    (For a start, that can't be done. If the function signature is A
    f(/*...*/); it can only return an object of class A. If it returns a
    *pointer* or *reference* to A, then indeed the referenced object can be
    of a derived class.)

    > So
    >you have to determine at the run time the return value is actually of
    >class B or class C . Besides RTTI, how can you achieve this?


    Why would you want to? The usual reason for having polymorphic classes
    is so that the same virtual functions in different subclasses can
    exhibit different behaviour, *without* the client code having to know
    anything about the polymorphism. Determining the actual type tends to
    subvert this abstraction.

    However, that's also the clue to how you can do this if you really must:
    just give the class a virtual function which returns some kind of type
    identifier.

    --
    Richard Herring
     
    Richard Herring, Mar 16, 2005
    #14
  15. Rolf Magnus Guest

    wrote:

    > I think it is quite useful in Polymorphism. For example, Class A has
    > two subclasses B and C.You have another function with return type as
    > class A, but the return value maybe an object of class B or class C. So
    > you have to determine at the run time the return value is actually of
    > class B or class C .


    However, you don't need the name as string for that to work. You only need a
    type identification, and you do get that.

    > Besides RTTI, how can you achieve this?


    Put the behavior that you need to depend on the actual type into a virtual
    member function of the class.
     
    Rolf Magnus, Mar 16, 2005
    #15
  16. Guest

    Suppose you have the following case:

    a class A (Array) which has two subclasses B(IntArray) and
    C(FloatArray). When a function returns an object of class A, the
    client code does't know it contains integers or floats. Now suppose A
    has a virtual function SetValueByIndex, the client code has to know
    the exactly type of A.

    Doing RTT here subverts polymorphism, but it seems that polyphosim
    cannot do in this case
     
    , Mar 16, 2005
    #16
  17. Guest

    wrote:
    > Hi Rolf,
    >
    > I tried. gcc 3.2 does not give the same as my source code as you said
    > it.
    > What can I do if I want to get the exactly the same name as I defined
    > in code?
    >
    > Rick



    Here's a solution that should work for gcc-3.2. Unfortunately it is
    not guaranteed to work generically:

    Bhat

    ///////////////////////////////////////////////////
    #include <iostream>
    #include <typeinfo>
    #include <cstring>


    using namespace std;


    #define TYPEID(x) cout << #x << ": " << strpbrk(typeid(x).name(), \
    "_abcdefghijklmnopqrstuvwxyzAB­CDEFGHIJKLMNOPQRSTUVWXYZ") << endl;

    struct sampleStruct
    {
    char c;
    short s;
    int i;
    };


    int
    main()
    {
    TYPEID(sampleStruct);

    sampleStruct whatsmytype;
    TYPEID(whatsmytype);
    }
     
    , Mar 16, 2005
    #17
  18. Guest

    Hi, Bhat:

    Your code is working for non pointer type. For pointer type, for
    example,
    TYPEID(&whatsmytype), gcc 3.3.2 give something begging with P
    "P12sampleStruct"

    Rick
     
    , Mar 16, 2005
    #18
  19. In message <>,
    "" <> writes

    [Please find out how to make Google quote what you're replying to...]

    >Suppose you have the following case:
    >
    >a class A (Array) which has two subclasses B(IntArray) and
    >C(FloatArray). When a function returns an object of class A,


    Bzzzt. A reference or pointer to an object of class A

    >the
    >client code does't know it contains integers or floats. Now suppose A
    >has a virtual function SetValueByIndex,


    What are you proposing as the argument type for the virtual function?

    SetValueByIndex(size_t index, ??? value);

    Remember that it has to have the same signature for both B and C. That
    will therefore have to be some kind of variant type that can contain
    either int or float. At that point your client code again no longer
    needs to worry about the details of B and C.

    >the client code has to know
    >the exactly type of A.
    >
    >Doing RTT here subverts polymorphism, but it seems that polyphosim
    >cannot do in this case
    >

    It sounds as though you're trying to reinvent some kind of "variant" or
    "any" class. It's likely that someone has already produced what you
    want.

    http://www.boost.org

    --
    Richard Herring
     
    Richard Herring, Mar 16, 2005
    #19
    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. John Harrison

    Re: RTTI

    John Harrison, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    452
    John Harrison
    Jul 14, 2003
  2. Alf P. Steinbach

    Re: RTTI

    Alf P. Steinbach, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    467
    Alf P. Steinbach
    Jul 14, 2003
  3. Steven Lien

    About RTTI

    Steven Lien, Aug 19, 2003, in forum: C++
    Replies:
    4
    Views:
    601
    Ivan Vecerina
    Aug 19, 2003
  4. BillyO
    Replies:
    2
    Views:
    490
    dslater
    Sep 30, 2003
  5. tirath
    Replies:
    3
    Views:
    710
    Ivan Vecerina
    Oct 12, 2003
Loading...

Share This Page