typeid behaving strangely

I'm trying to implement a virtual friend of sorts, in order to get the class name of an object (via typeid). However, when I set up a little test program for the feature, the name has an extra character in it; an integer representing the number of characters in the class name.
e.g. class Super; becomes
5Super
class Turtle; becomes
6Turtle


If anyone can point me towards a solution, it would be much appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;

//forward declaration
class Base;

//class Type exists to provide a friend function to class Base
class Type
{
    public:
    Type() {}
    ~Type() {}
    string get_name(Base * b);
};

class Base
{
    public:
    Base() { cout << "Base class ctor called.\n"; }
    virtual ~Base() { cout << "Base class dtor called.\n"; }
    //this function ensures that the friend Type::get_name is "inherited"
    virtual string my_type() { return typeid(*this).name(); };
    friend string Type::get_name(Base * b);
};

class Derived : public Base
{
    public:
    Derived() : Base() { cout << "Derived class ctor called.\n"; }
    ~Derived() { cout << "Derived class dtor called.\n"; }
};

string Type::get_name(Base * b) { return (*b).my_type(); };

int main()
{
    Base * b1 = new Base;
    Base * b2 = new Derived;

    Type t;
    t.add_name(b1);
    //this prints "4Base"
    t.print_name();

    t.add_name(b2);
    //and this prints "7Derived"
    t.print_name();

    delete b1;
    delete b2;

    return 0;
}


EDIT: I just copy/pasted the code provided on the type_info page on this site's reference section into a C::B project and I get the same issue.
Last edited on
the name given from typeid is at best a tool for debugging. It is all but worthless in an actual program because it's totally implementation dependent. You should not rely on it being at all consistent.
If that's the case, then could you suggest an alternate method for this?

I made a 2 player game, where each player can be either a person or a computer
(class Human or class Computer)

I wanted to make a save/load feature that would store all the necessary game data in a file. Two pieces of save data that I need are the class types for each player.

My original plan was, with the save feature, to get a string representing the class name (with the above code) and output that string to a save file. With the load feature, the program would read in those strings, then do something like:
1
2
3
4
if(read_string_type=="Human")
     return new Human(read_string_name);
else
     return new Computer(read_string_name);


to ensure that the same player types are used.
But now that's looking like a no-go :-(

What can I do instead
Oh wait, I could make a unique variable static to each class that represents the type?
1
2
3
4
5
6
7
8
9
10
11
class Human
{
     static const int type_ID = 1;
     //...
};

class Computer
{
     static const int type_ID = 2;
     //...
};


then

1
2
3
4
if(type_ID==1)
     return new Human(read_string_name);
else
     return new Computer(read_string_name);


EDIT: SUCCESS!!!!!1
Last edited on
Topic archived. No new replies allowed.