G
Guest
Ok... well... I'm a noob as far as templates go, and casting and such...
never bothered to learn much about this stuff... but now I've got a good
reason to care, so i'm just wondering what's going on in this code. I'm
assuming that T is *always* going to be some number (bool, INT16, int,
INT32, etc...) but no reason this shouldn't work on chars' and such... (...
*giggle* or pointers... could... work... on... pointers... don't make your
screen go pOp...)
template <class T> void SetValueOfT(typename T * &pT)
{
DWORD dwSetTo = 1;
pT = static_cast<T *>dwSetTo;
}
main(...)
{
int x = 0;
// x is 0
SetValueOfT(&x);
// now x is 1
bool y = false;
// y is false
SetValueOfT(&y);
// now y is true
}
In these cases... x will be 1, and y will be true... and that's great in a
"hey I want this to work today" kind of way... but I don't really understand
what's going on inside the SetValueOfT function. I'm guessing with x,
dwSetTo is being casted to an int before assigning the value to x... does
that mean that dwSetTo get's the value of 1, then a temporary variable is
created, of type (or at least sizeof) int, and given the value of 1, before
passing that value to x? Is there a way to prevent the copy from being made
(if it does get made...). I guess i could have made dwSetTo of type T to
speed things up, but then if I wanted to say pass dwSetTo to a few functions
before assinging it's value to pT, i'd have to cast alot more. Anyways... If
you feel like rambling, helping, tossing out links, complaining that I
didn't search hard enough (long as you don't start a full fledged flam ~.^)
please do, acause I'm hopeless and clueless right now. Also the more
detailed the better (talk ASM if yah want... I'd actually prefer to see
what's going on in ASM, since the code get's turned into that stuff anyways
^.~)
Ohh... one more thing before I forget... what's with the "typename T * &pT"
looks like for the x case it compresses to "int * &pT"... now I'd get it if
it was "int * pT" that's an int pointer... but I don't get the & before the
pT... I've seen it in a few headers, and havn't really got what it's all
about... My guess is it means pass the function a pointer to the variable,
but the function will pretend like it's playing with a variable... for
example:
// could also be...
// void blah(int* anInt)
void blah(int &anInt)
{
// could also be...
// (*anInt)++;
anInt++;
}
.... main (...)
{
int myInt = 5
blah(&myInt);
// myInt is now 6
}
These are all just (probably wrong 0.o) guesses though... so please correct
me and fill my head with your details
Thanks in advance,
Dead RAM
never bothered to learn much about this stuff... but now I've got a good
reason to care, so i'm just wondering what's going on in this code. I'm
assuming that T is *always* going to be some number (bool, INT16, int,
INT32, etc...) but no reason this shouldn't work on chars' and such... (...
*giggle* or pointers... could... work... on... pointers... don't make your
screen go pOp...)
template <class T> void SetValueOfT(typename T * &pT)
{
DWORD dwSetTo = 1;
pT = static_cast<T *>dwSetTo;
}
main(...)
{
int x = 0;
// x is 0
SetValueOfT(&x);
// now x is 1
bool y = false;
// y is false
SetValueOfT(&y);
// now y is true
}
In these cases... x will be 1, and y will be true... and that's great in a
"hey I want this to work today" kind of way... but I don't really understand
what's going on inside the SetValueOfT function. I'm guessing with x,
dwSetTo is being casted to an int before assigning the value to x... does
that mean that dwSetTo get's the value of 1, then a temporary variable is
created, of type (or at least sizeof) int, and given the value of 1, before
passing that value to x? Is there a way to prevent the copy from being made
(if it does get made...). I guess i could have made dwSetTo of type T to
speed things up, but then if I wanted to say pass dwSetTo to a few functions
before assinging it's value to pT, i'd have to cast alot more. Anyways... If
you feel like rambling, helping, tossing out links, complaining that I
didn't search hard enough (long as you don't start a full fledged flam ~.^)
please do, acause I'm hopeless and clueless right now. Also the more
detailed the better (talk ASM if yah want... I'd actually prefer to see
what's going on in ASM, since the code get's turned into that stuff anyways
^.~)
Ohh... one more thing before I forget... what's with the "typename T * &pT"
looks like for the x case it compresses to "int * &pT"... now I'd get it if
it was "int * pT" that's an int pointer... but I don't get the & before the
pT... I've seen it in a few headers, and havn't really got what it's all
about... My guess is it means pass the function a pointer to the variable,
but the function will pretend like it's playing with a variable... for
example:
// could also be...
// void blah(int* anInt)
void blah(int &anInt)
{
// could also be...
// (*anInt)++;
anInt++;
}
.... main (...)
{
int myInt = 5
blah(&myInt);
// myInt is now 6
}
These are all just (probably wrong 0.o) guesses though... so please correct
me and fill my head with your details
Thanks in advance,
Dead RAM