C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Real Time Type Information

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Benjamin GILLET
Guest





PostPosted: Mon Feb 27, 2006 5:06 pm    Post subject: Real Time Type Information Reply with quote



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
Back to top
Ben Pope
Guest





PostPosted: Mon Feb 27, 2006 6:06 pm    Post subject: Re: Real Time Type Information Reply with quote



Benjamin GILLET wrote:
Quote:
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.

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

Object::Object : m_oName("") {}

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

Object::Object : m_oName(p_oName) {}

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

string Object::getClassName()

// std::string Object::getClassName()

Quote:
{
string oClassName = "";
oClassName = typeid(*this).name();
return oClassName;

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

Quote:
}

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

// throw setName function away.

Quote:
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.

Quote:
- 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?

Quote:
Do you have any answer to these questions ?

Could you supply complete, compilable code?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Back to top
Rolf Magnus
Guest





PostPosted: Mon Feb 27, 2006 6:06 pm    Post subject: Re: Real Time Type Information Reply with quote



Benjamin GILLET wrote:

Quote:
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.

Quote:
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

Quote:
{
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


Quote:
{
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.

Quote:

- 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.

Quote:
Do you have any answer to these questions ?

Yes.
Back to top
Victor Bazarov
Guest





PostPosted: Mon Feb 27, 2006 6:06 pm    Post subject: Re: Real Time Type Information Reply with quote

Benjamin GILLET wrote:
Quote:
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...

Quote:
}

[..]

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.

Quote:
- 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'.

Quote:
Do you have any answer to these questions ?

Huh?

Quote:
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
--
Please remove capital As from my address when replying by mail
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.