 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bobo Guest
|
Posted: Wed Jan 21, 2004 9:19 am Post subject: virtual ~type_info(); |
|
|
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.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Jan 21, 2004 3:58 pm Post subject: Re: virtual ~type_info(); |
|
|
In message <745c4429.0401202241.6aadf69d (AT) posting (DOT) google.com>, Bobo
<pinzo (AT) correo (DOT) nu> writes
| Quote: | 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?).
|
Actually it is the copy ctor that is private (to inhibit copying). The
Standard does not actually list any other ctors but that makes sense
because type_info objects are created by the implementation. The reason
that a virtual dtor is provided is to allow vendors to provided extended
type_info objects that can be fully handled as references (there has to
be some virtual function for such things as RTTI to work)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (nee Spang Guest
|
Posted: Wed Jan 21, 2004 4:32 pm Post subject: Re: virtual ~type_info(); |
|
|
Good morning, Bobo!
Bobo schrieb:
| Quote: | 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?).
|
According to 18.5.1 the copy-constructor and the copy-assignment
operator are private,
not the default constructor of the type_info class. So basically your
precondition fails, and
one could derive from the class.
Greetings from Bremen,
Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Jan 21, 2004 7:48 pm Post subject: Re: virtual ~type_info(); |
|
|
Bobo wrote:
| Quote: | 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?).
|
The copy-constructor is private but it has a default constructor
too.
The implementation is allowed to define a derived class and to make
typeid() yield instances of this derived class. I suppose that on
such an implementation one could use:
static_cast<const extended_type_info &>(typeid(...))
(Yuck.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (nee Spang Guest
|
Posted: Thu Jan 22, 2004 9:15 am Post subject: Re: virtual ~type_info(); |
|
|
Hello, Bobo, again!
"Daniel Krügler (nee Spangenberg)" schrieb:
| Quote: | According to 18.5.1 the copy-constructor and the copy-assignment
operator are private,
not the default constructor of the type_info class. So basically your
precondition fails, and
one could derive from the class.
|
Strictly speaking, this explanation is not enough. As you know, the
default c'tor
is not automatically provided, if there are other c'tors declared (as
there are, see
the copy c'tor).
After reading it a second time I am not so sure any longer, that the
current
words are sufficient, because it strongly depends on the interpretation
on
class definitions presented in Our Holy Standard.
1.4.3 says:
"For classes and class templates, the library clauses specify partial
definitions.
Private members (clause 11) are not specified, but each implementation
shall
supply them to complete the definitions according to the description in
the library
clauses."
This makes me think, that std::type_info shall indeed not be
default-constructible.
I also did not find any other words in the description of type_info
related to
constructions other than
"Since the copy constructor and assignment operator for type_info are
private to
the class, objects of this type cannot be copied."
Since DefaultConstructable is not required, it seems to be an
implementation-specific
detail, whether this class has a default-constructor or not. The operator
typeid
returns l-values of type_info, so normal users don't know, **how** these
objects
have been constructed.
So finally I conclude from that, that only compiler writers are capable
of providing
derived classes for type_info (like extended_type_info). This reminds me,
that even
the Loki library, which also provides its own TypeInfo class, does not
derive from
type_info, but just contains a pointer to a type_info object.
Greetings from Bremen,
Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Jan 22, 2004 2:51 pm Post subject: Re: virtual ~type_info(); |
|
|
In message <400F7C44.518C51A4 (AT) bdal (DOT) de>, Daniel Krügler <dsp (AT) bdal (DOT) de>
writes
| Quote: | Hello, Bobo, again!
"Daniel Krügler (nee Spangenberg)" schrieb:
According to 18.5.1 the copy-constructor and the copy-assignment
operator are private,
not the default constructor of the type_info class. So basically your
precondition fails, and
one could derive from the class.
Strictly speaking, this explanation is not enough. As you know, the
default c'tor
is not automatically provided, if there are other c'tors declared (as
there are, see
the copy c'tor).
Put more simply, a default ctor is not compiler generated in the |
presence of ANY user declared ctor.
However, how does a user create an instance of a type_info? Implicitly
by defining a type. There is no other way to do so, nor should there be.
Nonetheless implementors must create such instances (one per appropriate
udt) and may want to provide extended type information, hence the
virtual dtor so that type_info is a polymorphic type.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bobo Guest
|
Posted: Thu Jan 22, 2004 2:59 pm Post subject: Re: virtual ~type_info(); |
|
|
Daniel Krügler (nee Spangenberg) <dsp (AT) bdal (DOT) de> wrote
| Quote: | Good morning, Bobo!
Bobo schrieb:
According to 18.5.1 the copy-constructor and the copy-assignment
operator are private,
not the default constructor of the type_info class. So basically your
precondition fails, and
one could derive from the class.
|
But AFAIK the default constructor of a class is only generated if
there is not any contructor declared for the class, but type_info does
have a constructor declaration.
Bobo.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bobo Guest
|
Posted: Fri Jan 23, 2004 9:47 am Post subject: Re: virtual ~type_info(); |
|
|
Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote
[snip]
| Quote: |
The copy-constructor is private but it has a default constructor
too.
|
I don't think so... see my other post...
| Quote: | The implementation is allowed to define a derived class and to make
typeid() yield instances of this derived class. I suppose that on
such an implementation one could use:
static_cast<const extended_type_info &>(typeid(...))
(Yuck.)
|
Maybe, but you won't require a virtual destructor to make a
static_cast.
But you'll need it in order to make type_info a polymorphic class so
you can write:
dynamic_cast<const extended_type_info &>(typeid(...))
or even
typeid(typeid(...)) == typeid(extended_type_info) // Cool!
But I'm not sure if any implementation do this. I guess I could test
it with the following line, using as some_type a simple type, an
array, a class, etc.:
std::cout << typeid(typeid(some_type)).name();
Bobo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Jan 24, 2004 12:52 pm Post subject: Re: virtual ~type_info(); |
|
|
Bobo wrote:
| Quote: | Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote in message
news:<slrnc0ta19.jc.do-not-spam-benh (AT) tin (DOT) bwsint.com>...
[snip]
The copy-constructor is private but it has a default constructor
too.
I don't think so... see my other post...
snip |
No, you're right. I had a dim and incorrect memory of some exception
that allows the default constructor to be implicitly declared if there
is a user-declared copy constructor and no other constructors. Since
I have had no reason to do define a class like that I've not noticed
that this is incorrect.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|