umm... something... template(s)... something else... pointer(s)... and such... 0.o yah, I'm hopeless

Discussion in 'C++' started by Guest, Oct 11, 2004.

  1. Guest

    Guest 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
     
    Guest, Oct 11, 2004
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
    > }


    I don't know where you got this code from but it has at least three errors
    and even when these are fixed it is completely misconcieved and will not do
    what your comments say it will do.

    I'm guessing that what you actually want is the much simpler code

    template <class T> void SetValueOfT(T * pT)
    {
    DWORD dwSetTo = 1;

    *pT = static_cast<T>(dwSetTo);
    }

    int 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
    }

    Since the posted code was bogus, so are the speculations on it, so I'm
    stopping here. If my code wasn't what you really intended then post some
    code that actually compiles and we'll try again.

    john
     
    John Harrison, Oct 11, 2004
    #2
    1. Advertising

  3. Guest

    Guest Guest

    "John Harrison" <> wrote in message
    news:...


    > I'm guessing that what you actually want is the much simpler code
    >
    > template <class T> void SetValueOfT(T * pT)
    > {
    > DWORD dwSetTo = 1;
    >
    > *pT = static_cast<T>(dwSetTo);
    > }
    >
    > int 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
    > }
    >
    > Since the posted code was bogus, so are the speculations on it, so I'm
    > stopping here. If my code wasn't what you really intended then post some
    > code that actually compiles and we'll try again.


    Sorry... wasn't thinking properly... very bogus...

    The above code (your new version of my old mess) is the code I intended to
    use. What's really important to me is what this template stuff ends up
    doing... in any sort of general way... it's hard to get to abstract with
    some of this stuff though ;) I guess I'm wondering if at compile time a
    function for each type (of T) is generated, and what kind of slowdowns or
    bulk and the sorts ends up in most peoples code. I've got all my templates
    working (... now I do anyways...), but I want to understand what was going
    on. I guessing, since this stuff could end up in a dll and the likes, that
    there is only one function created, unless you specify the "template < >
    SomeThing < aSpecialType > (...)". But all I really have is alot of
    guesswork and code that I bruteforced from a verbose compiler 0.o
     
    Guest, Oct 11, 2004
    #3
  4. [snip]

    > What's really important to me is what this template stuff ends up
    > doing... in any sort of general way... it's hard to get to abstract with
    > some of this stuff though ;) I guess I'm wondering if at compile time a
    > function for each type (of T) is generated,


    That's right.

    > and what kind of slowdowns or
    > bulk and the sorts ends up in most peoples code.


    A compiler/linker should be able to remove multiple copies of generated
    functions, ut this is a QOI issue of course.

    > I've got all my templates
    > working (... now I do anyways...), but I want to understand what was going
    > on. I guessing, since this stuff could end up in a dll and the likes, that
    > there is only one function created, unless you specify the "template < >
    > SomeThing < aSpecialType > (...)". But all I really have is alot of
    > guesswork and code that I bruteforced from a verbose compiler 0.o
    >


    It doesn't make sense to try an put a template in a DLL, since templates
    cannot be object code, only instantiations of templates are object code.
    Since templates don't get instantiated until they are used there is nothing
    to put in a DLL. This is the same reason that template code goes in header
    files not in source files.

    You can explicitly instantiate a template and that instantiated template
    could go in a DLL. But then you are limited to the instantiates you make
    ahead of time (when you compile the DLL).

    john
     
    John Harrison, Oct 11, 2004
    #4
  5. Guest

    Guest Guest

    Thanks John,

    Sorry for taking so long.. just got to coding... and forgot to get to
    thanking ;) You're a great help ;)
     
    Guest, Oct 13, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bGh1c2Vy?=

    Umm, what's that?

    =?Utf-8?B?bGh1c2Vy?=, Aug 4, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    327
    Kevin Spencer
    Aug 4, 2005
  2. ali
    Replies:
    6
    Views:
    338
    Rogier R. Spaans
    Dec 31, 2004
  3. Horst Kraemer

    hopeless scanf (or I'm hopeless :))

    Horst Kraemer, Nov 25, 2003, in forum: C Programming
    Replies:
    13
    Views:
    916
    Ekkehard Morgenstern
    Nov 26, 2003
  4. Helmut Jarausch

    Perl -> Python hopeless?

    Helmut Jarausch, Jul 18, 2003, in forum: Python
    Replies:
    3
    Views:
    473
    Hartmut Goebel
    Jul 23, 2003
  5. Old Fart

    Im'Hopeless At This

    Old Fart, Mar 3, 2008, in forum: HTML
    Replies:
    8
    Views:
    381
    C A Upsdell
    Mar 5, 2008
Loading...

Share This Page