dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31

Thread: Working with a database

  1. #16
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    simple. The array right now is:
    Code:
    Item ItemDef[4];
    When I add a new item to the list, I increase it (manually) to:
    Code:
    Item ItemDef[5];
    No programs are resizing anything (well, except my IDE), it's all hard-coded.


    Well, I have another question. This one might be a bit trickier. Would it be possible to save a function's name as a variable, and then use that variable to call the function? For example:
    Code:
    std::string functionVariable = "MyFunction()";
    
    //or
    
    specialvartype functionVariable = MyFunction();
    Can this be done, in part or in whole? This would let me store item scripts (as functions) as one of the variables in the definition. The alternative would be to store a script ID instead, and use the ID with a switch-case to determine which function to run. Any advice?
    Last edited by candlemaster; June 13th, 2010 at 09:01 PM. Reason: Added a question

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Working with a database

    Quote Originally Posted by candlemaster View Post
    simple. The array right now is:
    Code:
    Item ItemDef[4];
    When I add a new item to the list, I increase it (manually) to:
    Code:
    Item ItemDef[5];
    No programs are resizing anything (well, except my IDE), it's all hard-coded.
    Instead of this, why not do this programatically?

    The std::vector is resizable at runtime, so I don't see any need for doing what you're saying you're doing.
    Code:
    #include <vector>
    //...
    std::vector<Item> ItemDef;
    //...
    Then you can add a new item at runtime by just calling ItemDef.push_back(). Then you're not scrambling, recompiling a new executable every time.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Working with a database

    It could even be something as simple as this:
    Code:
    #include <fstream>
    //...
    std::ifstream infile("infile.txt");
    //..
    int num;
    // this file contains the number of items.
    //  Assume you've read this file, and the number is stored in the variable called "num"
    //...
    std::vector<Item> ItemDef(num);
    Now all you need to do is update the file, and not have to recompile your program if the number of items changes.

    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Well right now, the item list is defined in a .h file, so I have to recompile every time I change the list anyway.

    If I could store the item list in an external file, such as .txt, it would be very helpful.

  5. #20
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Working with a database

    Quote Originally Posted by candlemaster View Post
    If I could store the item list in an external file, such as .txt, it would be very helpful.
    Please strive to do that. Then there would be no need to recompile every time something changes.

    Regards,

    Paul McKenzie

  6. #21
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Any idea where i should start?

  7. #22
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Working with a database

    Place the information you want in a file, and read the information.

    Regards,

    Paul McKenzie

  8. #23
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Well, the best way I can think of to do that, I could just as easily do that with a .xml file, but from what I understand of those, reading them directly is really clumsy, plus there's a xml schema thing that I can use to make it easier (if I could figure it out that is...)

    Do you know of any good tutorials that'll help me out?

  9. #24
    Join Date
    Nov 2007
    Posts
    613

    Re: Working with a database

    Quote Originally Posted by candlemaster View Post
    Would it be possible to save a function's name as a variable, and then use that variable to call the function? For example:
    Code:
    std::string functionVariable = "MyFunction()";
     
    //or
     
    specialvartype functionVariable = MyFunction();
    For the first ideea, the answer is no. That's because the name of a function is not a string. From the programmer's perspective it looks like a text but from the compiler's perspective it's just the relative address where the body of the function can be foud. Probably the best definition would be to say that a function name it's a "named address" (do I have too much imagination ?)

    During the compilation all of the function names are replaced with their addresses. If you open the generated exe file with a binary editor, you'll find none of the names of your functions. You cannot relay on function names because they'll be all gone in the exe file.

    For the second ideea, the answer is yes.

    1. Declare a pointer type matching the declaration of your function:
    Code:
    typedef std::string (*MY_TYPE_PTR)();
    2. Declare a pointer variable of that type:
    Code:
    MY_TYPE_PTR pMyFunc;
    3. Assign the address of your function to that pointer variable:
    Code:
    pMyFunc = MyFunction;
    4. Call the function using the pointer variable:
    Code:
    pMyFunc();

  10. #25
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Quote Originally Posted by srelu View Post
    For the first ideea, the answer is no. That's because the name of a function is not a string. From the programmer's perspective it looks like a text but from the compiler's perspective it's just the relative address where the body of the function can be foud. Probably the best definition would be to say that a function name it's a "named address" (do I have too much imagination ?)

    During the compilation all of the function names are replaced with their addresses. If you open the generated exe file with a binary editor, you'll find none of the names of your functions. You cannot relay on function names because they'll be all gone in the exe file.

    For the second ideea, the answer is yes.

    1. Declare a pointer type matching the declaration of your function:
    Code:
    typedef std::string (*MY_TYPE_PTR)();
    2. Declare a pointer variable of that type:
    Code:
    MY_TYPE_PTR pMyFunc;
    3. Assign the address of your function to that pointer variable:
    Code:
    pMyFunc = MyFunction;
    4. Call the function using the pointer variable:
    Code:
    pMyFunc();

    I'm trying, but it seems the closest I'm getting gives me this error message:
    Code:
    cannot convert from 'void (__cdecl *)(void)' to 'scriptVar (__cdecl *)'
    Here's how I have it set:
    Just before the "Item" class definition:
    Code:
    typedef std::string *(scriptVar)();
    Inside the "Item" class definition:
    Code:
    scriptVar* itemScript;
    In the "Item()" function, I set it to NULL

    This is part of item #1's definition:
    Code:
    ItemDef[1].itemScript = &ItemScript_apple;
    "ItemScript_apple()" is the name of the function that I want to use.

    I tried using "&ItemScript_apple()" instead of "&ItemScript_apple", but I got
    Code:
    error C2102: '&' requires l-value
    as an error code.
    Last edited by candlemaster; June 15th, 2010 at 05:07 PM.

  11. #26
    Join Date
    Nov 2007
    Posts
    613

    Re: Working with a database

    Form what you wrote, I assumed that the declaration of your function is:
    Code:
    std::string MyFunction();
    It seems I was wrong. Please specify the right declaration.

  12. #27
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    Ah, I see. The function is a void, not a string.

  13. #28
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Working with a database

    Getting back to basics for a moment---

    Quote Originally Posted by candlemaster View Post
    If given the unique ID of an item (short int), I need to be able to use that to look up various statistics about the item,
    It sounds like you want a std::map, or a std::unordered_map (if your compiler supports it; if not, there's one in Boost).

    You could of course use an array or vector, if you can be sure that all of your unique IDs will be contiguous. The map option is preferable if you don't want restrictions on what those IDs can be other than "unique". I'm going to use the word "map" below for simplicity.

    without having to load the entire list of items into the memory.
    Well, one option that hasn't been suggested yet----store everything a file, as a series of records. But then, instead of loading the entire file into memory (presumably into objects stored in the map), you can parse the file once and simply store offsets into the file in the map instead. Then, when you look something up in the map, you just jump to the appropriate offset and read that part of the file.

    Will that be more efficient? I very much doubt it. But it would allow you to use a massive file without cluttering RAM up too much. Maybe that's worth it, I don't know.

  14. #29
    Join Date
    Jun 2010
    Posts
    17

    Re: Working with a database

    I changed "typedef std::string *(scriptVar)();" to "typedef void *(scriptVar)();", but nothing's changed...

  15. #30
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Working with a database

    Quote Originally Posted by candlemaster View Post
    I changed "typedef std::string *(scriptVar)();" to "typedef void *(scriptVar)();", but nothing's changed...
    Code:
    typedef void* (*scriptVar)();
    Syntax for typedef declaration of 'C"-style function pointer:
    Code:
    typedef return_type (*typedef_name)(args);
    Regards,

    Paul McKenzie

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)