home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: Having this=0, class-name: :main() and tasking in c++
- Message-ID: <1992Jul28.204329.4854@microsoft.com>
- Date: 28 Jul 92 20:43:29 GMT
- Organization: Microsoft Corporation
- References: <1DDA4ACFA5BF800C41@ursula.lucas.lu.se> <1992Jul27.050315.3154@sunb10.cs.uiuc.edu>
- Lines: 90
-
- In article <1992Jul27.050315.3154@sunb10.cs.uiuc.edu> pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
- >In <1DDA4ACFA5BF800C41@ursula.lucas.lu.se> KOSU_MATSB@ROUTH.KOSUFY.LU.SE writes:
- >
- >>Two (three) questions on C++:
- >
- >>Question #1: Having a 0 "this" pointer. Standard?
- >
- >*****> I don't know what you mean by "standard."
-
- I think the question being asked is:
-
- "Is it strictly legal to invoke a member function via a null pointer"
-
- ????
-
- IE
-
- Foo* pfoo = 0;
- ....
- pfoo->doSomething();
-
- I believe the answer is "NO it is NOT strictly legal, rather if you do this
- your results will be undefined."
-
- ARM page 53 states:
-
- "A postix expression followed by an arrow (->) followed by a 'name' is a
- postfix expression. The first expression must be a pointer to a class
- object and the 'name' must name a member of that class."
-
- It is not completely clear whether "pointer to a class object" *really means*
- "pointer to a class object" or rather
- "expression of type 'pointer to class object' "
-
- I take this to *really mean* "pointer to a class object"
-
- Note, for example, if doSomething() is a virtual function, then a given
- implementation might either cause a runtime error if "this" is null,
- or, if the implementation is "smart enough" to intuit the type of the
- actual object [what actual object therefore what actual type?] then possible
- the virtual dispatch would be optimized out.
-
- Note also, for a data member, pfoo->member is clearly ill-defined if pfoo
- is null.
-
- Also, note that in the presence of multiple inheritence, if the null
- invoking ptr is to remain "null" within an invoked member function, then
- at dispatch time there must be a null pointer test and fixup analogous
- to the null pointer fixup on pointer assignment. I don't believe compilers
- currently commonly do this null pointer fixup when dispatching to virtual
- functions -- nor would such a test and fixup on each dispatch be desirable.
-
- Try the below example, and see if your compiler doesn't doesn't perform
- this null-pointer fixup on dispatch. If it does, then you'll get all
- 1's output. If any none-1's are output, then your compiler doesn't
- do the null pointer fixup on dispatch.
-
- ====================================
-
- #include <iostream.h>
-
- class A
- {
- public:
- double am;
- int isNull() { return !this; }
- };
-
- class B
- {
- public:
- double bm;
- int isNull() { return !this; }
- };
-
- class C : public A, public B
- {
- public:
- int isNull() { return !this; }
- };
-
- main()
- {
- C* c = 0;
-
- cout << c->A::isNull() << '\n';
- cout << c->B::isNull() << '\n';
- cout << c->C::isNull() << '\n';
- return 0;
- }
-