Topic: Is this code completely legal?
Author: olczyk@interaccess.com (Thaddeus L. Olczyk)
Date: 2000/03/01 Raw View
I've tried to look it up in the standard but couldn't find appropriate
references. ( A gripe with the standard for anyone who is listening,
the pdf can't find hyphenated words like base-clause which appear in
the grammer ).
class A
{
public:
void DoSomething()
{
MessageBox(0, typeid(*this).name(), "",MB_OK);
}
// virtual char *Type(){ return "InA";}
};
class :public A // "anonymous" class, btw what is this called?
{
public:
// virtual char *Type(){ return "InA";}
} b; // definiton of variable in class declaration ( actually
// definition in this case ).
b.DoSomething();
A a;
a.DoSomething();
Also the compiler I used genreates code so that the MessageBox
displays the letter A in both cases.If I uncomment the virtual
function in the definition of, then it generates code which displays
the letter A first, then the word class. Is this standard behaviour?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/02 Raw View
"Thaddeus L. Olczyk" wrote:
>
> I've tried to look it up in the standard but couldn't find appropriate
> references. ( A gripe with the standard for anyone who is listening,
> the pdf can't find hyphenated words like base-clause which appear in
> the grammer ).
You mean acrobat reader can't find it. A bigger gripe is that the
standard got misformatted in it's troff->PDF conversion and there
are some bogus hyphenation that shouldn't be there.
> class :public A // "anonymous" class, btw what is this called?
Not really anonymous, just unnamed. There aren't really anonymous
classes. You can either define a class name, of declare a variable
or typedef or both, but the standard won't let you do neither:
class {
int a;
};
is illegal.
>
> Also the compiler I used genreates code so that the MessageBox
> displays the letter A in both cases.If I uncomment the virtual
> function in the definition of, then it generates code which displays
> the letter A first, then the word class. Is this standard behaviour?
Yes, dynamic typing only works when you have at least one virtual function.
The idea is that there is overhead associated with dynamic typing and
so it is disabled except when you actually signal you are using polymorphism
by declaring something virtual.
If you look in 5.2.8 it says (2nd paragraph)...when typeid is applied to an
lvalue expression whose type is a polymorphic class type the result refers
to a type_info object representing the type of the most derived object
(that is the dynamic type). (3rd paragraph) [otherwise] type_info object
representing the static type of the expression.
A polymorhpic class is defined in 10.3 as one that defines or inherits a
virtual member.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/03/02 Raw View
"Thaddeus L. Olczyk" wrote:
>
> I've tried to look it up in the standard but couldn't find appropriate
> references. ( A gripe with the standard for anyone who is listening,
> the pdf can't find hyphenated words like base-clause which appear in
> the grammer ).
It can - just leave out the hyphen in the search string.
> class A
> {
> public:
> void DoSomething()
> {
> MessageBox(0, typeid(*this).name(), "",MB_OK);
> }
> // virtual char *Type(){ return "InA";}
> };
>
> class :public A // "anonymous" class, btw what is this called?
> {
> public:
> // virtual char *Type(){ return "InA";}
> } b; // definiton of variable in class declaration ( actually
> // definition in this case ).
>
> b.DoSomething();
> A a;
> a.DoSomething();
>
> Also the compiler I used genreates code so that the MessageBox
> displays the letter A in both cases.If I uncomment the virtual
> function in the definition of, then it generates code which displays
> the letter A first, then the word class. Is this standard behaviour?
The value returned by std::typeinfo::name() is implementation defined,
it can be anything the implementation chooses. It could even be a
constant "" for all types. A decent compiler will do much better than
that, but I wouldn't recommend depending upon the contents in portable
code. I would include it in error messages, in the hopes that it will be
informative, but don't let the actual behavior of your code depend upon
it. In particular, always use before(), operator==(), or operator!=() to
compare typeid()'s; don't try to use the name() for that purpose.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: roger_orr@my-deja.com
Date: 2000/03/02 Raw View
In article <38bea685.168397140@nntp.interaccess.com>,
olczyk@interaccess.com wrote:
> I've tried to look it up in the standard but couldn't find appropriate
> references.
In section "5.2.8 Type identification" it talks about typeid of
polymorphic and non-polymorphic classes.
[See 10.3.1 "A class that declares or inherits a virtual function is
called a polymorphic class."]
For non-polymorphic ones like your sample code the _static_ type is
returned - so "A" is correct for both calls.
For polymorphic objects the typeid refers to the most derived type - in
your case I guess "class" is used as the name since it is anonymous.
As far as I can tell the behaviour of the code is correct.
Roger Orr.
> class A
> {
> public:
> void DoSomething()
> {
> MessageBox(0, typeid(*this).name(), "",MB_OK);
> }
> // virtual char *Type(){ return "InA";}
> };
>
> class :public A // "anonymous" class, btw what is this called?
> {
> public:
> // virtual char *Type(){ return "InA";}
> } b; // definiton of variable in class declaration ( actually
> // definition in this case ).
>
> b.DoSomething();
> A a;
> a.DoSomething();
>
> Also the compiler I used genreates code so that the MessageBox
> displays the letter A in both cases.If I uncomment the virtual
> function in the definition of, then it generates code which displays
> the letter A first, then the word class. Is this standard behaviour?
>
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/03/02 Raw View
Ron Natalie wrote:
>
> "Thaddeus L. Olczyk" wrote:
> >
> > I've tried to look it up in the standard but couldn't find appropriate
> > references. ( A gripe with the standard for anyone who is listening,
> > the pdf can't find hyphenated words like base-clause which appear in
> > the grammer ).
>
> You mean acrobat reader can't find it.
Actually, it can. You just need to know how to ask. :-)
The hyphen that appears in the PDF text is not the same code as
the hyphen on your keyboard. If you copy-paste text containing
a hyphen (or just the hyphen character) to the "find" window,
you can search successfully for it.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 2000/03/02 Raw View
Steve Clamage <stephen.clamage@sun.com> wrote in message
news:38BDA108.CA699D54@sun.com...
> The hyphen that appears in the PDF text is not the same code as
> the hyphen on your keyboard. If you copy-paste text containing
> a hyphen (or just the hyphen character) to the "find" window,
> you can search successfully for it.
Yeah, but I don't have no copyable document (why in the world did I hurry
up?) so I don't have any hyphen in the darned clipboard to start with.
Andrei
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Anatoli Tubman <anatoli@my-deja.com>
Date: 2000/03/06 Raw View
Andrei Alexandrescu wrote:
> Yeah, but I don't have no copyable document (why in the world did I
hurry
> up?) so I don't have any hyphen in the darned clipboard to start with.
>
> Andrei
Try to use *space* instead of hyphen. It works for me.
Apparently, Acrobat Reader treats space in the search
field as [^a-zA-Z0-9]*. Or something like that.
--
Regards
Anatoli (anatoli<at>ptc<dot>com) opinions aren't
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]