C++ casting

K

keith

Hello,

Could someone please explain why the (GCC, ancient 2.95.3 version)
compiler won't let me static_cast<> a char* to a unsigned char* or
vice versa? It also won't accept dynamic_cast<> for those
conversions. The only one it will permit is reinterpret_cast<>.

BTW, yes I know that pointer casts are 'evil', but here's a for-
example: ostream::write for some reason expects a const char* and a
streamsize parameter, rather than the rather more logical const void*
and a size_t. Many of my applications are mucking around with raw
binary data either in vector<unsigned char> or sometimes in raw
unsigned char[], so when I come to write the data out to a file, I
have to cast it to a char*.

Here's a simple example of the problem:

void uc_func(unsigned char *x) {}

void c_func(char *x){}

int main()
{
unsigned char uc[10];
char c[10];

uc_func(uc);
uc_func(static_cast<unsigned char*>(c));

c_func(static_cast<char*>(uc));
c_func(c);
}
---------------------------------
g++ -g -o x x.cc
x.cc: In function `int main()':
x.cc:17: static_cast from `char *' to `unsigned char *'
x.cc:19: static_cast from `unsigned char *' to `char *'
make: *** [xx] Error 1

g++ -g -o x x.cc
xx.cc: In function `int main()':
xx.cc:17: cannot dynamic_cast `c' (of type `char[10]') to type
`unsigned char *'
xx.cc:19: cannot dynamic_cast `uc' (of type `unsigned char[10]') to
type `char *'
make: *** [xx] Error 1
 
R

Robert Bauck Hamar

Could someone please explain why the (GCC, ancient 2.95.3 version)
compiler won't let me static_cast<> a char* to a unsigned char* or
vice versa?

Static_cast is only for related types. Char* and unsigned char* are two
unrelated pointer types.
It also won't accept dynamic_cast<> for those
conversions.

Why should it? There's no run time information in char, signed char or
unsigned char that would help dynamic_cast. And the types are not class
types in the same inheritance tree.
The only one it will permit is reinterpret_cast<>.

And C-style casts.
 
N

Neelesh Bodas

Hello,

Could someone please explain why the (GCC, ancient 2.95.3 version)
compiler won't let me static_cast<> a char* to a unsigned char* or
vice versa? It also won't accept dynamic_cast<> for those
conversions. The only one it will permit is reinterpret_cast<>.

you cannt use static_cast for casting pointers from one type to
another unless one of them is void* or they are related by
inheritance.

It wont accept dynamic cast either since the two pointers are not
related by inheritance.
BTW, yes I know that pointer casts are 'evil', but here's a for-
example: ostream::write for some reason expects a const char* and a
streamsize parameter, rather than the rather more logical const void*
and a size_t. Many of my applications are mucking around with raw
binary data either in vector<unsigned char> or sometimes in raw
unsigned char[], so when I come to write the data out to a file, I
have to cast it to a char*.

Here's a simple example of the problem:

void uc_func(unsigned char *x) {}

void c_func(char *x){}

int main()
{
unsigned char uc[10];
char c[10];

uc_func(uc);
uc_func(static_cast<unsigned char*>(c));


try this: uc_func(static_cast said:
c_func(static_cast<char*>(uc));
c_func(static_cast said:
c_func(c);}


---------------------------------
g++ -g -o x x.cc
x.cc: In function `int main()':
x.cc:17: static_cast from `char *' to `unsigned char *'
x.cc:19: static_cast from `unsigned char *' to `char *'
make: *** [xx] Error 1

g++ -g -o x x.cc
xx.cc: In function `int main()':
xx.cc:17: cannot dynamic_cast `c' (of type `char[10]') to type
`unsigned char *'
xx.cc:19: cannot dynamic_cast `uc' (of type `unsigned char[10]') to
type `char *'
make: *** [xx] Error 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top