Strange casting problem w/ GCC 3.3

D

destroyedlolo

Hi all,

I just upgrade my workstation to Gcc 3.3, and I got a very strange
probleme with casting : I got an error if I try to get the adress of a
"casted" pointer.


---- dumy example source code ------

class tata {
int a;
};

int main(int, char **){
void *toto;
tata **tst_ptr = &((tata *)toto);
}

-------------------------------

[faillie]/home/faillie/src $ g++ tst.cxx
tst.cxx: In function `int main(int, char**)':
tst.cxx:7: error: non-lvalue in unary `&'

I duno if it's normal or not (I don't see why the compiler think this
casted stuff is not a "lvalue"), but do you know how to compile such
thing ?

If it's a GCC bug, I'm very bad as it's the only version I have on my
HPUX workstation :-(

Thanks for your help

Laurent
 
S

Shane Beasley

Alternatively, you could use a reference, which is an lvalue and thus
has an address you can take:

void *toto;
tata **tst_ptr = &reinterpret_cast<tata *&>(toto);

Hmm... My code was meant to do what the OP asked to do, which is not
the same as what the code above does. Oops. :)

However, since conversion from tata * to void * is implicit, the
reverse can (and should) be done with static_cast, not
reinterpret_cast:

tata *converted_toto = static_cast<tata *>(toto);

I would agree that this is the sanest route, all things considered.

- Shane
 
D

destroyedlolo

Hello,
casting toto into a tata* creates a nameless temporary (the "casted
stuff") that contains the converted value. And this temporary is not an
lvalue. You cannot take the address of it.

For me it's a "surprise" for me, because doing such cast, I said to
the compiler "ok, even if toto is a void *, please treat it as a tata
*" ... as it done w/ normal C or w/ older version of the compiler. If
I want realy a modification of my object, I said dynamic_cast<> or
something like that.

Thanks for your light and code.

Bye

Laurent
 
M

Michiel Salters

casting toto into a tata* creates a nameless temporary (the "casted
stuff") that contains the converted value. And this temporary is not an
lvalue. You cannot take the address of it.


Write the result of the conversion into an own variable and take a
pointer to that, and try to avoid old-style casts:

void *toto;
tata *converted_toto = reinterpret_cast<tata *>(toto);
tata **tst_ptr = &converted_toto;

That compiles but won't work as expected. You'd want
(*tst_ptr) = new tata;
to set the void* to the new tata. After all, tst_ptr should
point to toto.

Regards,
 
R

Rolf Magnus

destroyedlolo said:
Hello,


For me it's a "surprise" for me, because doing such cast, I said to
the compiler "ok, even if toto is a void *, please treat it as a tata
*"

That's not what a cast is doing. A cast converts the specified value to
the destination type and returns the result of that conversion.
... as it done w/ normal C or w/ older version of the compiler.

Which compiler? Btw, "normal C" behaves the same as C++ wrt this.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top