Real Time Type Information

B

Benjamin GILLET

Dear all,

I have coded a class called Object with a member method called
getClassName().

Here is the code of this class:



#include <iostream>
#include "Object.h"

Object::Object()
{
this->setName("");
}

Object::Object(const string &p_oName)
{
this->setName(p_oName);
}

Object::~Object()
{
std::cout << "Destruction of object named (" << this->getName() << ") of
type (" << this->getClassName() << ")" << std::endl;
}

string Object::getClassName()
{
string oClassName = "";
oClassName = typeid(*this).name();
return oClassName;
}

void Object::setName(const string &p_oName)
{
this->m_oName = p_oName;
}

string Object::getName()
{
return this->m_oName;
}



I made a class named DummyObject inheriting from Object.
This class just contains constructors.
Below is the code of the test program.



#include <iostream>
#include "DummyObject.h"

int main()
{
std::cout << "TestObject: start ..." << std::endl;
std::cout << "TestObject: try creating object ..." << std::endl;
Object anObject("anObject");
std::cout << "TestObject: object created ..." << std::endl;
std::cout << "TestObject: class of object named (" << anObject.getName()
<< ") is (" << anObject.getClassName() << ") ... " << std::endl;
std::cout << "TestObject: try creating dummy object ..." << std::endl;
DummyObject aDummyObject("aDummyObject");
std::cout << "TestObject: dummy object created ..." << std::endl;
std::cout << "TestObject: class name of object named (" <<
aDummyObject.getName() << ") is (" << aDummyObject.getClassName() << ") ...
" << std::endl;
std::cout << "TestObject: stop ..." << std::endl;
return 0;
}



Output of the program is:



TestObject : start ...
TestObject : try creating object ...
TestObject : object created ...
TestObject : class of object named (anObject) is (6Object) ...
TestObject : try creating dummy object ...
TestObject : dummy object created ...
TestObject : class of object named (aDummyObject) is (11DummyObject) ...
TestObject : stop ...
Destruction of object named (aDummyObject) of type (6Object)
Destruction of object named (anObject) of type (6Object)



My questions are :

- Why do I have a number in front of class name instead of having 'class
Object' ?

- Why do I have the same classname for both objects when they are destroyed
while I have correct classnames when the getClassName() method is called
from the test program ?



Do you have any answer to these questions ?


I am coding under Windows XP SP2 using DJGPP 4.0.1 (gpp). I tried with
Visual C++ 2005. I don't have the first problem but still the second
(destructor message).

Best regards

Benjamin GILLET
 
V

Victor Bazarov

Benjamin said:
Dear all,

I have coded a class called Object with a member method called
getClassName().

Here is the code of this class:
[..]
string Object::getClassName()
{
string oClassName = "";
oClassName = typeid(*this).name();
return oClassName;

You could have just written

return typeid(*this).name();

and got rid of the local variable...
}

[..]

Output of the program is:

TestObject : start ...
TestObject : try creating object ...
TestObject : object created ...
TestObject : class of object named (anObject) is (6Object) ...
TestObject : try creating dummy object ...
TestObject : dummy object created ...
TestObject : class of object named (aDummyObject) is (11DummyObject) ...
TestObject : stop ...
Destruction of object named (aDummyObject) of type (6Object)
Destruction of object named (anObject) of type (6Object)



My questions are :

- Why do I have a number in front of class name instead of having 'class
Object' ?

Because your compiler chose to put it there.
- Why do I have the same classname for both objects when they are destroyed
while I have correct classnames when the getClassName() method is called
from the test program ?

Because in the Object's _destructor_ the type is 'Object'.
Do you have any answer to these questions ?
Huh?

I am coding under Windows XP SP2 using DJGPP 4.0.1 (gpp). I tried with
Visual C++ 2005. I don't have the first problem but still the second
(destructor message).

Whatever.

V
 
R

Rolf Magnus

Benjamin said:
Dear all,

I have coded a class called Object with a member method called
getClassName().

Here is the code of this class:



#include <iostream>
#include "Object.h"

Object::Object()
{
this->setName("");
}

You should remove that "this->" part. It's unnecessary clutter.
Object::Object(const string &p_oName)
{
this->setName(p_oName);
}

Object::~Object()
{
std::cout << "Destruction of object named (" << this->getName() << ")
of
type (" << this->getClassName() << ")" << std::endl;
}

string Object::getClassName()

Should be:

string Object::getClassName() const
{
string oClassName = "";
oClassName = typeid(*this).name();
return oClassName;
}

void Object::setName(const string &p_oName)
{
this->m_oName = p_oName;
}

string Object::getName()

Should be:

string Object::getName() const

{
return this->m_oName;
}



I made a class named DummyObject inheriting from Object.
This class just contains constructors.
Below is the code of the test program.



#include <iostream>
#include "DummyObject.h"

int main()
{
std::cout << "TestObject: start ..." << std::endl;
std::cout << "TestObject: try creating object ..." << std::endl;
Object anObject("anObject");
std::cout << "TestObject: object created ..." << std::endl;
std::cout << "TestObject: class of object named (" <<
anObject.getName()
<< ") is (" << anObject.getClassName() << ") ... " << std::endl;
std::cout << "TestObject: try creating dummy object ..." << std::endl;
DummyObject aDummyObject("aDummyObject");
std::cout << "TestObject: dummy object created ..." << std::endl;
std::cout << "TestObject: class name of object named (" <<
aDummyObject.getName() << ") is (" << aDummyObject.getClassName() << ")
... " << std::endl;
std::cout << "TestObject: stop ..." << std::endl;
return 0;
}



Output of the program is:



TestObject : start ...
TestObject : try creating object ...
TestObject : object created ...
TestObject : class of object named (anObject) is (6Object) ...
TestObject : try creating dummy object ...
TestObject : dummy object created ...
TestObject : class of object named (aDummyObject) is (11DummyObject) ...
TestObject : stop ...
Destruction of object named (aDummyObject) of type (6Object)
Destruction of object named (anObject) of type (6Object)



My questions are :

- Why do I have a number in front of class name instead of having 'class
Object' ?

What makes you think it should be 'class Object'? The string returned by
typeid::name() is implementation-defined, i.e. compilers can give you
anything they want.
- Why do I have the same classname for both objects when they are
destroyed while I have correct classnames when the getClassName() method
is called from the test program ?

Because you print the name from the destructor of the base class. When that
destructor is called, the derived part of the object has already been
destroyed, so the object is not any longer of that derived class.
Do you have any answer to these questions ?

Yes.
 
B

Ben Pope

Benjamin said:
Dear all,

I have coded a class called Object with a member method called
getClassName().

Here is the code of this class:



#include <iostream>
#include "Object.h"


// Missing file.
Object::Object()
{
this->setName("");
}

Object::Object : m_oName("") {}
Object::Object(const string &p_oName)
{
this->setName(p_oName);
}

Object::Object : m_oName(p_oName) {}
Object::~Object()
{
std::cout << "Destruction of object named (" << this->getName() << ") of
type (" << this->getClassName() << ")" << std::endl;
}

string Object::getClassName()

// std::string Object::getClassName()
{
string oClassName = "";
oClassName = typeid(*this).name();
return oClassName;

return std::string(typeid(*this).name());
}

void Object::setName(const string &p_oName)
{
this->m_oName = p_oName;
}

// throw setName function away.
string Object::getName()
{
return this->m_oName;
}



I made a class named DummyObject inheriting from Object.
This class just contains constructors.
Below is the code of the test program.



#include <iostream>
#include "DummyObject.h"

int main()
{
std::cout << "TestObject: start ..." << std::endl;
std::cout << "TestObject: try creating object ..." << std::endl;
Object anObject("anObject");
std::cout << "TestObject: object created ..." << std::endl;
std::cout << "TestObject: class of object named (" << anObject.getName()
<< ") is (" << anObject.getClassName() << ") ... " << std::endl;
std::cout << "TestObject: try creating dummy object ..." << std::endl;
DummyObject aDummyObject("aDummyObject");
std::cout << "TestObject: dummy object created ..." << std::endl;
std::cout << "TestObject: class name of object named (" <<
aDummyObject.getName() << ") is (" << aDummyObject.getClassName() << ") ...
" << std::endl;
std::cout << "TestObject: stop ..." << std::endl;
return 0;
}



Output of the program is:



TestObject : start ...
TestObject : try creating object ...
TestObject : object created ...
TestObject : class of object named (anObject) is (6Object) ...
TestObject : try creating dummy object ...
TestObject : dummy object created ...
TestObject : class of object named (aDummyObject) is (11DummyObject) ...
TestObject : stop ...
Destruction of object named (aDummyObject) of type (6Object)
Destruction of object named (anObject) of type (6Object)



My questions are :

- Why do I have a number in front of class name instead of having 'class
Object' ?

Why not? typeid.name can return anything it likes.
- Why do I have the same classname for both objects when they are destroyed
while I have correct classnames when the getClassName() method is called
from the test program ?

Probably a problem in the code you didn't supply. Missing virtuals on
the destructor, perhaps?
Do you have any answer to these questions ?

Could you supply complete, compilable code?

Ben Pope
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top