casting to a voldemort type
Alex via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Mar 19 08:10:56 PDT 2016
Finally. A question about Voldemort types :)
I have the following class, with an accompanying function. Skip
the code to the link for a runnable version.
/*--- code begin ---*/
class roof
{
int huhu = 9;
void* funVoldemort(size_t my_size)
{
auto gg = huhu;
if(my_size < 5)
{
return new size_t(3);
}
struct Vold
{
int begin;
int end;
bool b;
int ff()
{
return gg;
}
}
return new Vold();
}
}
void rara_fun(void* ptr, uint mysize)
{
if(mysize < 5)
{
writeln("rara_fun: ", *(cast(size_t*)ptr));
}
else
{
//???
}
}
/*--- code end ---*/
see
http://dpaste.dzfl.pl/2e361adf04b6
for a runnable copy.
First of all: it is a little bit unsatisfying, that the
funVoldemort is not working with an auto or auto ref return type.
But I see, the types are different after all. And, to solve this
part I changed the return type to a void*.
The question is, how to write the accompanied function in the
part, which should cast the pointer to the voldemort type?
What I already thought about is: writing a union inside the
struct, which is either my long or some other data. But this is
in general not possible, as the "other data" could be together
greater, then the long.
The obvious solution would be: to define the struct outside the
function. But the point is, that these structs only defines
behavior and the data inside them is just auxiliary data to pass
the queries to the real data storages.
The question is motivated by the following:
I try to write some recursive structure. And if the accompanying
function is called, it passes the question to a voldemort object.
The voldemort object by itself doesn't store the information but
knows, how to reduce the "my_size" parameter and which other
voldemort object to ask. This goes on, till the real data is
accessed.
More information about the Digitalmars-d-learn
mailing list