 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
BekTek Guest
|
Posted: Fri Oct 15, 2004 11:28 am Post subject: Why we need void poiter casting...? |
|
|
When I worked in VS.net 2003 enviornment..
I realized that all pointer variable can be implicitly casted to void *..
It seem to be so error-prone.. Am I right?
Actually I made this kind of codes..
class Foo{
};
main(){
Foo foo;
cout << &foo;
}
I thought that the code above might have caused compile-time error..
but I was compiled without any error even though I never overrided >>
operator for Foo*.
It is kind of weird..
Can you tell me implicit pointer casting is standard or not?
And is it usefull?
[ 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: Fri Oct 15, 2004 10:42 pm Post subject: Re: Why we need void poiter casting...? |
|
|
In article <v%Fbd.3114$n57.2177@trnddc01>, BekTek <bektek (AT) gmail (DOT) com>
writes
| Quote: | When I worked in VS.net 2003 enviornment..
I realized that all pointer variable can be implicitly casted to void *..
It seem to be so error-prone.. Am I right?
|
That it is correct C++ or that it is error prone?
It is correct C++ and it is not generally considered to be particularly
error prone. Automatic conversion from void* to type* (as allowed in C)
is much more error prone and not allowed in C++ where conversions from
void* must use an explicit cast.
| Quote: |
Actually I made this kind of codes..
class Foo{
};
main(){
Foo foo;
cout << &foo;
}
I thought that the code above might have caused compile-time error..
but I was compiled without any error even though I never overrided
operator for Foo*.
|
And it is perfectly OK, but the compiler does not convert &foo to void*,
it converts it to bool in this case.
| Quote: |
It is kind of weird..
Can you tell me implicit pointer casting is standard or not?
From any* to void*, yes. There are also allowances for implicit |
conversions between base and derived pointers.
| Quote: | And is it usefull?
|
Well it saves scattering casts around ones code.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Fri Oct 15, 2004 10:49 pm Post subject: Re: Why we need void poiter casting...? |
|
|
Hi,
| Quote: | I realized that all pointer variable can be implicitly casted to void *..
It seem to be so error-prone.. Am I right?
|
A "void *" provides less guarantees than any other pointer, thus, it
is safe to convert any pointer to a "void *" without destroying
program correctness. Guarantees given by void * are also given by any other
pointer.
What you're observing is that, actually, there exists an operator<<
that only requires the very basic guarantees of the void *. While
possibly not in the intended way, the program runs provably correctly
and "stable" for possibly all pointers.
Thus, I wouldn't say that the implicit conversion is guilty, but rather
providing an ostream interface that requires so little guarantees that it
is satisfied by almost any input. And *that* is confusing.
| Quote: | Can you tell me implicit pointer casting is standard or not?
|
It is standard.
void *'s are usually only required for low-level interfaces, or in
generics where one would only require "any kind of pointer you like",
asuming that the type of object it points to is available by some other
side information. As such, the void * is quite useful, and implicit
conversions are required to make such code working. Whether it is useful
to have an operator<< that acts on a void * as right operand is then
another question.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Oct 15, 2004 11:01 pm Post subject: Re: Why we need void poiter casting...? |
|
|
BekTek wrote:
| Quote: | I thought that the code above might have caused compile-time error..
but I was compiled without any error even though I never overrided
operator for Foo*.
You can't override operator >> for pointer types (it must be either a |
class or enum type).
The operator<<(void*) for ostreams is PREPARED to deal with any sort of
pointer, it just prints the address (it's defined to be the same as the
%p in printf).
Maybe you should argue that operator<< having a void* overload makes for
errors?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Fri Oct 15, 2004 11:12 pm Post subject: Re: Why we need void poiter casting...? |
|
|
BekTek wrote:
| Quote: | When I worked in VS.net 2003 enviornment..
I realized that all pointer variable can be implicitly casted to void *..
It seem to be so error-prone.. Am I right?
|
Yes. Pointers can be implicitly cast to (cv) void*.
As many things in C++ (and in real life too!) void* can be used
judiciously or not. Error-proneness comes from the use you make of it.
void* is not error-prone per se, IMHO.
| Quote: |
It is kind of weird..
|
It doesn't look at all weird to me. It has always been like this since
void was invented.
| Quote: | Can you tell me implicit pointer casting is standard or not?
|
Yes, it is (4.10 para 2).
| Quote: | And is it usefull?
|
I would say yes.
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Fri Oct 15, 2004 11:23 pm Post subject: Re: Why we need void poiter casting...? |
|
|
Hi,
BekTek wrote:
| Quote: | Can you tell me implicit pointer casting is standard or not?
|
Yes, implicit casting to void* (with compatible const or volatile
modifiers) is OK:
int *pi = ...;
void *pv = pi;
It does not, however, work the other way round:
pi = pv; // error
pi = static_cast<int*>(pv); // OK
| Quote: | And is it usefull?
|
It may prove useful when you need to call some API function that expects
void * parameter(s). I suppose it is allowed because there were already
tons of existing code using it exactly this way.
However, IMHO, it should not be allowed in terms of coding guidelines,
preferring that all pointer casts are explicit.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Sat Oct 16, 2004 10:09 am Post subject: Re: Why we need void poiter casting...? |
|
|
Hi,
Maciej Sobczak wrote:
| Quote: | However, IMHO, it should not be allowed in terms of coding guidelines,
preferring that all pointer casts are explicit.
|
To correct myself, implicit casts frmo pointer to derived to pointer to
base are of course OK. The above coding guideline is applicable when
casting to void* and back (where explicit cast is required anyway) -
this is simply for symmetry and code self-dofumentation.
Please note also that many low-level function use either void* or char*
parameters (sometimes even for the same concepts, if you compare
different libraries or different APIs). Whereas implicit cast to void*
is allowed by the standard, it is not when casting to char*. This is one
of the reasons to avoid implicit cast to void* when calling low-level
APIs for the sake of code consistency.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Oct 18, 2004 6:25 pm Post subject: Re: Why we need void poiter casting...? |
|
|
Alberto Barbati <AlbertoBarbati (AT) libero (DOT) it> wrote
| Quote: | BekTek wrote:
And is it usefull?
I would say yes.
|
Actually, in a purely C++ context, I'm not sure that it is that useful.
It wouldn't be that much of a pain to need an explicit cast in the rare
cases where we did want to throw out type information.
In real life, of course, the world doesn't stop at C++, and we usually
have to interface to external API's defined in terms of C. Where it is
useful.
--
James Kanze GABI Software http://www.gabi-soft.fr
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Oct 18, 2004 6:33 pm Post subject: Re: Why we need void poiter casting...? |
|
|
Ron Natalie <ron (AT) sensor (DOT) com> wrote
| Quote: | BekTek wrote:
I thought that the code above might have caused compile-time error..
but I was compiled without any error even though I never overrided
operator for Foo*.
You can't override operator >> for pointer types (it must be either a
class or enum type).
|
One of the parameters must be either a class or an enum type (or a
reference to a class or an enum type). Since the first parameter is
almost certainly std::istream&, the condition is met, and you can
override operator>> with a pointer as the second type.
| Quote: | The operator<<(void*) for ostreams is PREPARED to deal with any sort
of pointer, it just prints the address (it's defined to be the same as
the %p in printf).
Maybe you should argue that operator<< having a void* overload makes
for errors?
|
It probably does, but I doubt that they are very frequent. And they are
easy to recognize.
One could argue that the whole type system is a little weak, with far
too many implicit conversions. I'm sure that there are more errors due
to people doing things like:
s /* an std::string */ += 3.14 ;
But that's part of the price we pay for being based on C.
--
James Kanze GABI Software http://www.gabi-soft.fr
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
[ 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
|
|