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