can a class display its name?

This is a discussion on can a class display its name? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to have a class display its name (i.e. base) without setting a varible to it's name? ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    can a class display its name?

    Is there a way to have a class display its name (i.e. base) without setting a varible to it's name? Maybe there is a variable that has contains its name?

    Code:
    class base {
    int x,y;
    
    public:
    void dispname();
    };
    
    base::dispname() {
    std::cout << "the classes name is" <<  ???? //possible varible containing "base"
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I believe you can get it with RTTI, though the syntax for structure with that data has escaped my memory.

    Why do you want this name though? Is it for use with inheritance issues? If so, there are likely better ways of dealing with your problems (RTTI is rather slow), but we'd need to know more about the problem to come up with a solution.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    There's a function in <typeinfo>:
    typeid(obj).name(),
    which returns a string representation of the type name of the object. You could probably use it. I'm not sure. Do research on that if you'd like.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    I wouldn't think so....

    But, I hadn't heard of <typeinfo> before, and maybe that header puts the names in the execuatable ??? Normally you won't find the class names (or variable names) in your exe file.

    I looked at the Dinkumware web site, and they say a name, not the name. Well, it's worth a try!

    Normally, you can call your class MyClass, or MySuperLongClassName, and you will get exactly the same execuitable.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    That's what RTTI is for, with RTTI enabled, class names, etc are placed into your EXE file as you have typed them into your program - eg. MyClass, or MySuperLongClassName. Then you can use the typeinfo stuff for retrieving class names, etc.

    RTTI stands for Run Time Type Information I believe. I only ever use it for the "dynamic_cast" operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21