Topic: Pointers and ostreams
Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/11/29 Raw View
Fergus Henderson (fjh@murlibobo.cs.mu.OZ.AU) wrote:
: jpotter@falcon.lhup.edu (John E. Potter) writes:
: >Fergus Henderson (fjh@mundook.cs.mu.OZ.AU) wrote:
: >: The statement
: >: cout << this;
: >: will be legal in a member function only if the object's static type is
: >: not `const', i.e. only if the member function is not a const member
: >: function.
: >
: >More please. My compilers have a void const* operator<<; however, CD1
: >shows void*. Was this intended or a typo?
: I have no idea whether the original I/O streams programmer left
: the const out deliberately or by accident. I think it should take
: `const void *' rather than `void *'. However, I also know that
: the committee has been made aware of this issue, and there doesn't
: seem to be a consensus in favour of changing it.
: >My only compiler which supports bool prefers to convert pointers to
: >bool rather than void*. An uncast pointer prints as 1. Thoughts?
: I think your compiler is broken. The draft standard says
: "a conversion that is not a conversion of a pointer .... to bool
: is better than another conversion that is such a conversion".
: Hence, converting a pointer to `void *' should be better than
: converting it to bool.
Yes, thank you. Humm, however, converting (this) SomeClass const* to
bool is possible while to void* is not. So printing _this_ in a
non-const member function produces the pointer value while in a const
member it produces 1 (or true). Interesting.
John
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "joe (j.) halpin" <jhalpin@nortel.ca>
Date: 1996/11/24 Raw View
In article <576r81$47d@mulga.cs.mu.OZ.AU>,
Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
>"joe (j.) halpin" <jhalpin@nortel.ca> writes:
>
>>Is it legal to send a 'this' pointer to an arbitrary object to an
>>ostream?
>
[...]
>You only have to cast it if the pointer is a pointer to const,
>or a `char *' pointer. Except for the `char *' case, it will
>ordinarily be converted to `void *' automatically, unless it
>is const. For a `char *' pointer, it will of course print
>the string pointed to, rather than printing the pointer itself.
>(Of course, the `this' pointer is never of type `char *'.)
How about pointers to other objects within a const function? We also
get errors about this. For example:
extern Y *y;
void X::somefunc() const
{
cout << y << '\n';
}
Should this be a problem? AIX C++ takes it, but HP C++
doesn't. Unfortunatly I don't speak standardese well enough to be
confident about what I think.
Thanks
Joe
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/11/25 Raw View
Fergus Henderson (fjh@mundook.cs.mu.OZ.AU) wrote:
: "joe (j.) halpin" <jhalpin@nortel.ca> writes:
: >Is it legal to send a 'this' pointer to an arbitrary object to an
: >ostream?
: The statement
: cout << this;
: will be legal in a member function only if the object's static type is
: not `const', i.e. only if the member function is not a const member
: function.
More please. My compilers have a void const* operator<<; however, CD1
shows void*. Was this intended or a typo?
: >My understanding is that, if you want to print the value of a pointer,
: >you have to cast it to (void *). If this is not done, what is the
: >result?
: You only have to cast it if the pointer is a pointer to const,
: or a `char *' pointer. Except for the `char *' case, it will
: ordinarily be converted to `void *' automatically, unless it
: is const. For a `char *' pointer, it will of course print
: the string pointed to, rather than printing the pointer itself.
: (Of course, the `this' pointer is never of type `char *'.)
My only compiler which supports bool prefers to convert pointers to
bool rather than void*. An uncast pointer prints as 1. Thoughts?
John
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/26 Raw View
jpotter@falcon.lhup.edu (John E. Potter) writes:
>Fergus Henderson (fjh@mundook.cs.mu.OZ.AU) wrote:
>: The statement
>: cout << this;
>: will be legal in a member function only if the object's static type is
>: not `const', i.e. only if the member function is not a const member
>: function.
>
>More please. My compilers have a void const* operator<<; however, CD1
>shows void*. Was this intended or a typo?
I have no idea whether the original I/O streams programmer left
the const out deliberately or by accident. I think it should take
`const void *' rather than `void *'. However, I also know that
the committee has been made aware of this issue, and there doesn't
seem to be a consensus in favour of changing it.
>My only compiler which supports bool prefers to convert pointers to
>bool rather than void*. An uncast pointer prints as 1. Thoughts?
I think your compiler is broken. The draft standard says
"a conversion that is not a conversion of a pointer .... to bool
is better than another conversion that is such a conversion".
Hence, converting a pointer to `void *' should be better than
converting it to bool.
| 13.3.3.2 Ranking implicit conversion sequences [over.ics.rank]
|
| 4 Standard conversion sequences are ordered by their ranks: an Exact
| Match is a better conversion than a Promotion, which is a better con-
| version than a Conversion. Two conversion sequences with the same
| rank are indistinguishable unless one of the following rules applies:
|
| --A conversion that is not a conversion of a pointer, or pointer to
| member, to bool is better than another conversion that is such a
| conversion.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/26 Raw View
"joe (j.) halpin" <jhalpin@nortel.ca> writes:
>extern Y *y;
>
>void X::somefunc() const
>{
> cout << y << '\n';
>}
>
>Should this be a problem? AIX C++ takes it, but HP C++ doesn't.
Well, your example is incomplete, but think that with the appropriate
declarations it should be OK. The following program, which is pretty
similar, is strictly conforming to the draft C++ standard, I'm pretty
sure.
#include <iostream.h>
struct Y {} *y = 0;
int main() {
cout << y << '\n';
return 0;
}
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "joe (j.) halpin" <jhalpin@nortel.ca>
Date: 1996/11/23 Raw View
Is it legal to send a 'this' pointer to an arbitrary object to an
ostream?
My understanding is that, if you want to print the value of a pointer,
you have to cast it to (void *). If this is not done, what is the
result?
Thanks
Joe
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/23 Raw View
"joe (j.) halpin" <jhalpin@nortel.ca> writes:
>Is it legal to send a 'this' pointer to an arbitrary object to an
>ostream?
The statement
cout << this;
will be legal in a member function only if the object's static type is
not `const', i.e. only if the member function is not a const member
function.
>My understanding is that, if you want to print the value of a pointer,
>you have to cast it to (void *). If this is not done, what is the
>result?
You only have to cast it if the pointer is a pointer to const,
or a `char *' pointer. Except for the `char *' case, it will
ordinarily be converted to `void *' automatically, unless it
is const. For a `char *' pointer, it will of course print
the string pointed to, rather than printing the pointer itself.
(Of course, the `this' pointer is never of type `char *'.)
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]