Thread: Alternative to typedef?

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Unhappy Alternative to typedef?

    Hey everyone, I have a problem. This is what I'm trying to do (but a lot simplified):
    Code:
    int a;
    //stuff
    void myFunction()
    {
         if(a == 1)
              typedef char data_type;
         else if(a == 2)
              typedef short data_type;
         else
              typedef long data_type;
    }
    
    void function2()
    {
         data_type* x = (data_type*)myData; //myData is a void*
         //do stuff with x
    }
    that's the basic idea of what I want to do, but this won't work since typedef can't be used inside a function's definition. Is there any alternative way to achieve the same purpose? (function2 is always called after myFunction)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    typedefs DO work in functions.
    Last edited by Polymorphic OOP; 01-05-2003 at 07:01 PM.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Really? But off the MSDN library on my comp, it says:
    You cannot use the typedef specifier inside a function definition.
    Is there some hidden meaning I'm missing?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    if you declare a typedef inside a function it's going to only be local to that function

    EDIT: also, since your typedef's are in the if blocks, they are only valid from WITHIN those if blocks. Besides, typedefs are compile-time, not runtime, which is a point that I think you are missing.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    oh... I see. But is there any way to achieve a similar effect with what I intended to happen actually happening? (i.e. so I won't have to do the if/else-ing in function2, which gets called several hundred times more frequently than myFunction does )

    **EDIT**
    Well, actually the function names are:
    myFunction() = lock()
    function2() = setPixel()
    Last edited by Hunter2; 01-05-2003 at 07:08 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    depends, is the variable 'a' going to vary at runtime?

    EDIT: Seems to me like you want templating anyways. If not, if a never changes at runtime, you can use #define, but I don't recommend that.
    Last edited by Polymorphic OOP; 01-05-2003 at 07:13 PM.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, maybe but not too much: a = the bpp of a ddraw surface
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    then make the funtion templated and put the function2() calls in the if/elses blocks of the function that CALLS function2()

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, I don't really follow you. What do you mean by templated?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    don't mean to be rude, but maybe you should learn C++ before you toy with DirectX.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >typedefs DO work in functions.
    Not according to the standard:
    Code:
    7.1.3 The typedef specifier
    
    Declarations containing the decl-specifier typedef declare identifiers that can be used later for naming
    fundamental (3.9.1) or compound (3.9.2) types. The typedef specifier shall not be used in a function-
    definition (8.4), and it shall not be combined in a decl-specifier-seq with any other kind of specifier
    except a typespecifier.
    -Prelude
    My best code is written with the delete key.

  12. #12
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Prelude
    >typedefs DO work in functions.
    Not according to the standard:
    Code:
    7.1.3 The typedef specifier
    
    Declarations containing the decl-specifier typedef declare identifiers that can be used later for naming
    fundamental (3.9.1) or compound (3.9.2) types. The typedef specifier shall not be used in a function-
    definition (8.4), and it shall not be combined in a decl-specifier-seq with any other kind of specifier
    except a typespecifier.
    -Prelude
    *Runs home crying*

    I guess that's just another folly of msvc++ then. It compiles fine and works "properly" over here.

    Maybe I should learn C++ then

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I have learned c++, or at least enough to get by. And the question I just asked is not a matter of DirectX-work-or-not-work, but a matter of convenience and maybe performance... I guess if this is too advanced for me (*sniff*) I'll just skip this bit

    typedef's are compile-time and not run-time
    Yeah, I sort of figured that out after some experimentation... well, I wasn't sure but it really looked like it, based on compiler behaviour.
    Last edited by Hunter2; 01-05-2003 at 07:52 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Code:
    int a;
    void* myData;
    
    template<class data_type>
    void function2()
    {
         data_type* x = (data_type*)myData;
    }
    
    void myFunction()
    {
         if(a == 1)
             function2<char>();
         else if(a == 2)
             function2<short>();
         else
             function2<long>();
    }

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, I see. That would be a great help But, unfortunately, myFunction() isn't supposed to call function2(). Would this work instead?
    Code:
    int a;
    void* myData;
    void (*pFunction2) ();
    
    template<class data_type>
    void function2()
    {
         data_type* x = (data_type*)myData;
    }
    
    void myFunction()
    {
         if(a == 1)
             pfunction2 = function2<char>;
         else if(a == 2)
             pfunction2 = function2<short>;
         else
             pfunction2 = function2<long>;
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM