Re: reference to pointer
Jonathan Lee wrote:
Hi Ralf,
Hi Jonathan,
void foo(someClass* s) {
someOtherClass* &pr(reinterpret_cast<someOtherClass*>(s->ptr));
/* ?... */
}
The result of reinterpret_cast<>() is an rvalue. It can't be
referenced, or used to initialize a reference. That's more or less
what the error message is saying.
Okay.
Note also that reinterpret_cast<>() is pretty much an all around bad
idea. Especially for circumventing the type system, as you seem to
have done. Personally, I would have define a constructor for
someOtherClass() that accepted your char*, then create an instance of
your object and act on that. Copying it back out if need be. If your
code truly runs "a zillion" times (heh) this shouldn't be costly to
do.
I don't have a choice. foo() is a "User Defined Function" for our MySQL
server. someClass (actually called initid) is a structure that is used
to communicate with the server. The manual says:
char *ptr
A pointer that the function can use for its own purposes. For example,
functions can use initid->ptr to communicate allocated memory among
themselves. xxx_init() should allocate the memory and assign it to
this pointer: initid->ptr = allocated_memory; In xxx() [my function
foo] and xxx_deinit(), refer to initid->ptr to use or deallocate the
memory.
And I really use that function a zillion times, its result is used in an
on clause of a self join of a table that can have up to 40,000 entries.
In a first version of that function I was so silly to allocate and
deallocate the memory in foo() itself because I thought the strings the
function takes as arguments can vary in length. After realizing that in
foo_init() I am told about the maximum length and moving (de)allocation
out to foo_init() and foo_deinit() the select statement was about 100
times faster. But it still needs quite a lot of time that's why I'm
anxious to avoid the creation of variables. They probably cause much
less harm than newing an deleting objects. But still if I can get away
with aliases (to avoid the repeated casts), I'd like to do so.
Ralf