473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Real Time Type Information

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
Feb 27 '06 #1
3 2148
Benjamin GILLET wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 27 '06 #2
Benjamin GILLET wrote:
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.

Feb 27 '06 #3
Benjamin GILLET wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
1
by: asj | last post by:
Just when I say web services may be hyped, here comes a news report that says Swedish Referendum Results will be delivered in real time over the internet using Java web services. In one sense,...
3
by: Aaron | last post by:
is there a program that allows me to debug a webiste written in c# or vb in real time? It would provide me with the list of resources that the website is using. (varaibles name, type, size,...
4
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a...
2
by: x | last post by:
hi i am a pilot by profession. i want to create a database of my logbook using ms access 2002. i am facing a problem regarding the format of time field. when i select "Data/Time" data type for my...
26
by: Charles Law | last post by:
What I mean is, I want my web client to be updated in real-time. The scenario is that I have a database that is updated asynchronously, and when the update takes place I want to 'notify' my web...
9
by: Leslie Coover | last post by:
http://finance.yahoo.com/ offers a wide range of financial data. Would anyone have (or know where I can get) a sample of code that
5
by: kalyxo | last post by:
Hi all! For one of our web-communities we plan an offer of real time stock quotes incl. advanced notification services. I have the following questions: - Do you know of any services, that...
0
by: Johannes Nix | last post by:
Hi, this might be of interest for people who are look for practical information on doing real-time signal processing, possibly using multiple CPUs, and wonder whether it's possible to use...
2
by: Blubaugh, David A. | last post by:
To All, I have done some additional research into the possibility of utilizing Python for hard real time development. I have seen on various websites where this has been discussed before on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.