473,381 Members | 1,450 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,381 software developers and data experts.

type_info

Hi Everyone,

I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,
class type_info {
public:
_CRTIMP virtual ~type_info();
_CRTIMP int operator==(const type_info& rhs) const;
_CRTIMP int operator!=(const type_info& rhs) const;
_CRTIMP int before(const type_info& rhs) const;
_CRTIMP const char* name() const;
_CRTIMP const char* raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);
};
the constructor and assignment operator is private, why is it so?

Thanks in advance!!!
Dec 8 '07 #1
9 3346
On Dec 8, 10:38 am, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,

I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,

class type_info {
public:
_CRTIMP virtual ~type_info();
_CRTIMP int operator==(const type_info& rhs) const;
_CRTIMP int operator!=(const type_info& rhs) const;
_CRTIMP int before(const type_info& rhs) const;
_CRTIMP const char* name() const;
_CRTIMP const char* raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);

};

the constructor and assignment operator is private, why is it so?

Thanks in advance!!!
I believe it so closely coupled with usage of typeid operator and the
specific implementation (by the compiler) that you would not normally
be able to do anything meaninful with it, even if you could create an
object of type_info. The only way to get a type_info object is by
usage of typeid().

It would be interesting to know, why you want to create an object of
it?
Dec 8 '07 #2
On Dec 8, 11:08 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 8, 10:38 am, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,
I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,
class type_info {
public:
_CRTIMP virtual ~type_info();
_CRTIMP int operator==(const type_info& rhs) const;
_CRTIMP int operator!=(const type_info& rhs) const;
_CRTIMP int before(const type_info& rhs) const;
_CRTIMP const char* name() const;
_CRTIMP const char* raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);
};
the constructor and assignment operator is private, why is it so?
Thanks in advance!!!

I believe it so closely coupled with usage of typeid operator and the
specific implementation (by the compiler) that you would not normally
be able to do anything meaninful with it, even if you could create an
object of type_info. The only way to get a type_info object is by
usage of typeid().

It would be interesting to know, why you want to create an object of
it?
Nothing really specific, Was just playing around it and i wonder why
the copy constructor isn't private...
Dec 8 '07 #3
On Dec 8, 11:55 am, Rahul <sam_...@yahoo.co.inwrote:
On Dec 8, 11:08 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:


On Dec 8, 10:38 am, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,
I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,
class type_info {
public:
_CRTIMP virtual ~type_info();
_CRTIMP int operator==(const type_info& rhs) const;
_CRTIMP int operator!=(const type_info& rhs) const;
_CRTIMP int before(const type_info& rhs) const;
_CRTIMP const char* name() const;
_CRTIMP const char* raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);
};
the constructor and assignment operator is private, why is it so?
Thanks in advance!!!
I believe it so closely coupled with usage of typeid operator and the
specific implementation (by the compiler) that you would not normally
be able to do anything meaninful with it, even if you could create an
object of type_info. The only way to get a type_info object is by
usage of typeid().
It would be interesting to know, why you want to create an object of
it?

Nothing really specific, Was just playing around it and i wonder why
the copy constructor isn't private...- Hide quoted text -
So that you can return a local object from a function by-value?
Dec 8 '07 #4
Rahul wrote:
>>...
class type_info {
public:
_CRTIMP virtual ~type_info();
_CRTIMP int operator==(const type_info& rhs) const;
_CRTIMP int operator!=(const type_info& rhs) const;
_CRTIMP int before(const type_info& rhs) const;
_CRTIMP const char* name() const;
_CRTIMP const char* raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);
};
...
It would be interesting to know, why you want to create an object of
it?

Nothing really specific, Was just playing around it and i wonder why
the copy constructor isn't private...
Huh? What are you taking about? In the definition you provided the copy
constructor obviously _is_ private.

--
Best regards,
Andrey Tarasevich
Dec 8 '07 #5
On 2007-12-08 00:38:10 -0500, Rahul <sa*****@yahoo.co.insaid:
>
I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,
It's designed that way because you don't need to copy them (use
references or pointers), and having a single instance for each type
makes equality comparisons much simpler.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 8 '07 #6
On Dec 8, 4:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-08 00:38:10 -0500, Rahul <sam_...@yahoo.co.insaid:
I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,

It's designed that way because you don't need to copy them (use
references or pointers), and having a single instance for each type
makes equality comparisons much simpler.
I noticed this now that the copy constructor is private. And since it
has been declared, one doesn't get the default c-tor. This is strange.
Creating the object of type_info on your own might seem useless/
redundant, but why is the copy construction not allowed? Isn't that
like forcing people to create dynamic objects of type_info and then
returning a pointer to them where the deallocation responsibility lies
with the caller (you can't even pass an object as an argument that the
function could make change to by assigning to it)? Is that good
design? The copy c-tor should have been allowed so that one could
return a local type_info object by value to the caller. Same goes for
assignment.
Dec 8 '07 #7
On 2007-12-08 09:23:05 -0500, Abhishek Padmanabh
<ab****************@gmail.comsaid:
On Dec 8, 4:37 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2007-12-08 00:38:10 -0500, Rahul <sam_...@yahoo.co.insaid:
>>I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,

It's designed that way because you don't need to copy them (use
references or pointers), and having a single instance for each type
makes equality comparisons much simpler.

I noticed this now that the copy constructor is private. And since it
has been declared, one doesn't get the default c-tor. This is strange.
Creating the object of type_info on your own might seem useless/
redundant, but why is the copy construction not allowed?
As I said, it makes equality comparisons much simpler.
Isn't that
like forcing people to create dynamic objects of type_info and then
returning a pointer to them where the deallocation responsibility lies
with the caller (you can't even pass an object as an argument that the
function could make change to by assigning to it)?
No. Just use the reference that type_id returns. Memory management for
type_id objects is not your responsibility. (In fact, there's typically
one static object for each type, so there's no dynamic memory involved.
But that's up to the implementation)
Is that good
design?
Sure. Why wouldn't it be?
The copy c-tor should have been allowed so that one could
return a local type_info object by value to the caller. Same goes for
assignment.
Why do you want a copy of a type_info object? Just use the reference
that type_id returns, or take its address and use that pointer.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 8 '07 #8
On Dec 8, 8:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-08 09:23:05 -0500, Abhishek Padmanabh
<abhishek.padman...@gmail.comsaid:
On Dec 8, 4:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-08 00:38:10 -0500, Rahul <sam_...@yahoo.co.insaid:
>I was working with Run Time Type Information and with typeid
operator, and so on i tried to create an object of type_info class and
i'm not able to do so, because of the class design,
It's designed that way because you don't need to copy them (use
references or pointers), and having a single instance for each type
makes equality comparisons much simpler.
I noticed this now that the copy constructor is private. And since it
has been declared, one doesn't get the default c-tor. This is strange.
Creating the object of type_info on your own might seem useless/
redundant, but why is the copy construction not allowed?

As I said, it makes equality comparisons much simpler.
Isn't that
like forcing people to create dynamic objects of type_info and then
returning a pointer to them where the deallocation responsibility lies
with the caller (you can't even pass an object as an argument that the
function could make change to by assigning to it)?

No. Just use the reference that type_id returns. Memory management for
type_id objects is not your responsibility. (In fact, there's typically
one static object for each type, so there's no dynamic memory involved.
But that's up to the implementation)
Ok, got it now. Thanks! I was missing the point about typeid returning
a static object. I thought memory management was upto the user but as
you said - it's not. Dynamic memory might be involved as well but
since it would be one per type. I guess that's fine as per the below
quote from the standards - [expr.typeid]/1: "The result of a typeid
expression is an lvalue of static type const std::type_info (18.6.1)
and dynamic type const std::type_info or const name where name is an
implementation-defined class derived from std :: type_info which
preserves the behavior described in 18.6.1.62) The lifetime of the
object referred to by the lvalue extends to the end of the program.
Whether or not the destructor is called for the std::type_info object
at the end of the program is unspecified."
Dec 8 '07 #9
On Dec 8, 4:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-08 09:23:05 -0500, Abhishek Padmanabh
<abhishek.padman...@gmail.comsaid:
On Dec 8, 4:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-08 00:38:10 -0500, Rahul <sam_...@yahoo.co.insaid:
[...]
Isn't that like forcing people to create dynamic objects of
type_info and then returning a pointer to them where the
deallocation responsibility lies with the caller (you can't
even pass an object as an argument that the function could
make change to by assigning to it)?
No. Just use the reference that type_id returns. Memory
management for type_id objects is not your responsibility. (In
fact, there's typically one static object for each type, so
there's no dynamic memory involved. But that's up to the
implementation)
One of the most frequence uses of type_info (about the only one
I can think of off hand, in fact) is as an index into a map.
And because the key type must in fact be std::type_info const*,
that you have to write your own comparison function for map.
The standard could have provided one. And now that hash tables
are being added to the standard, I hope the standard provides
for some means of getting a hash code of a type_info---there's
practically no way of doing it yourself.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 9 '07 #10

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

Similar topics

54
by: sks_cpp | last post by:
What is a type_info function, more particularly, what is a type_info node? Are these related to the vtable by any means? I have seen linker errors such as: "undefined reference to SomeClass...
5
by: skscpp | last post by:
I "think" I understand why a virtual table needs to be constructed when polymorphic classes are being compiled. Now, I also know what a type_info function and type_info node are although I am...
3
by: Bobo | last post by:
Hello all. Can anybody explay WTH std::type_info has a virtual destructor? AFAIK the constructor is private so you cannot inherit from it (who would want, anyway?). TIA. Bobo.
1
by: tham | last post by:
hi, with RTTI turned on... can i keep the reference to the type_info so that i can pass it around? i want to pass something to a function that will pick out a specific type of object from a pool...
1
by: jahhaj | last post by:
Lots of people seem to have problems with these, here's my particular variation. I'm using a third partly library which I believe was compiled with VC 7.1. I'm using the free version of VC 8 and...
1
by: Daniel Kraft | last post by:
Hi all, I tried to store a std::type_info value in my code so I didn't have to call typeid more than once when checking if a polymorphic object is of one of certain types, like this: #include...
2
by: sumsin | last post by:
What is type_info object?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.