Thread: Suggestions and question about bool pointers

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Suggestions and question about bool pointers

    Made a new thread 'cause I suspected no one would read the other one, 'cause I said it was fixed. Don't hate me, please. xP

    http://codepad.org/opw4HeWp

    Now, I'd like suggestions on this and is there anyway to check if a pointer, like the used pFrameFunc pointer, has been assigned anything else than 'garbage' ? Like, if it has been assigned by the code, and not some default compiler thing...

    And for the suggestions, I mean would this be the right place for the loop? I don't want the users of this engine to make the loop themselves...
    And I use the variable "bool& failed" as the condition for the loop, there is no problem with this? I mean, it is a pointer... Just to make me more sure about this!
    Thanks in advance!
    Currently research OpenGL

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What you have posted makes no sense with respect to the question, since you deleted pFrameFunc and any reference to it, et cetera.

    Since it was (as I recall) passed in to your function, then it would have to have come from somewhere. If you want to make sure that it always has a valid value, then you want to set it to something in the constructor, presumably.

    I'm pretty sure you never want a constructor to loop infinitely.

    And no, your failed variable is not a pointer.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    well, this constructor will be called like LIQUID* liq; then later when all is set to go, liq = new LIQUID(kinda many arguments); personally I see no flaw in this, but I've been wrong plenty before
    Currently research OpenGL

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Code:
    LIQUID* liq;
    SYSTEM sys;
    
    int main(){
    
        sys.SetFrameFunc(SOME_DEFINED_FUNCTION());
    
        bool initCheck;
    
        liq = new LIQUID(initCheck, sys, ect...);
        if(initCheck) return true;
    
        return 0;
    }
    Just a short and fast example of how it will be called(most likely)
    Currently research OpenGL

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Akkernight View Post
    well, this constructor will be called like LIQUID* liq; then later when all is set to go, liq = new LIQUID(kinda many arguments); personally I see no flaw in this, but I've been wrong plenty before
    This constructor won't be called when you do LIQUID *liq, since you aren't creating a LIQUID object at that time. When you do call the constructor, you don't get kinda many arguments. You get two arguments, since that's what your constructor takes. Notice that that means, in particular, that you will never get to set up your callback function, since that's not one of your arguments, and you will never get a chance to call your set-up function, since no more lines of your main program will ever execute (since you said that this function should run until shutdown).

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    If I understand you correctly, I understand that you think I'm a complete retard...

    The engine isn't done yet, infact far from. What that sketch showed, was how it WILL be.
    Also, I've changed too much of the design for all before this post to be really 'valid' to consider once you post, or... I dunno how to explain, but the code is changed.

    But I do have one question... If I call " return pFrameFunc; " does it run through pFrameFunc? Meaning, it runs the function that pFrameFunc points to ?
    Currently research OpenGL

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Akkernight View Post
    The engine isn't done yet, infact far from. What that sketch showed, was how it WILL be.
    Also, I've changed too much of the design for all before this post to be really 'valid' to consider once you post, or... I dunno how to explain, but the code is changed.
    Well, fair enough, but then asking questions about the code that isn't there isn't going to get you very far.
    But I do have one question... If I call " return pFrameFunc; " does it run through pFrameFunc? Meaning, it runs the function that pFrameFunc points to ?
    If you do return 5; does C++ try to run the function at memory address 5? Or does it just return the value 5?

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    that's a number... Won't return need to run the function to check or something? Or do I need to make a RunFrameFunc() function too ?

    EDIT: And when I posted this, I didn't have a plan to change it or anything, but I did change it after your reply, it was just so that all future posts think of that.. Well, that kinda changes the question of the topic :S
    Last edited by Akkernight; 03-21-2009 at 07:53 PM.
    Currently research OpenGL

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Akkernight View Post
    that's a number...
    So is pFrameFunc. What's your point?
    Quote Originally Posted by Akkernight View Post
    Won't return need to run the function to check or something? Or do I need to make a RunFrameFunc() function too ?
    Return doesn't "need to" do anything. If you want the function to be called, then call it. There's no need to make yet another function to pass the function pointer to and so on infinitely; just stick parentheses at the end of it like any other function.

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ... I want to run the pointer, like, call pFrameFunc somewhere, and it runs the function which pFrameFunc points to, also the reason I asked if I was to make a new function is 'cause I amde a new class called SYSTEM which has this stuff now, and the pointers are private....
    Currently research OpenGL

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have access to the pointer, then you can run it. If pFrameFunc is now a private member of sys, then you're going to have to run it through that object, yes. Of course, in this case it wouldn't be possible to even consider "return pFrameFunc;" since pFrameFunc would not be available to us to use (being a member of some other class).

  12. #12
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ok, thanks!
    Currently research OpenGL

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>sys.SetFrameFunc(SOME_DEFINED_FUNCTION());
    Note that this will run SOME_DEFINED_FUNCTION and pass what is returned from that function to SetFrameFunc.
    If you want to assign a function pointer, then take its address instead of calling it:
    sys.SetFrameFunc(&SOME_DEFINED_FUNCTION);

    2nd) I don't think it's a good idea the for constructor to block. Not at all. The constructor should only initialize the object, making it ready for use. Create some other function, such as Run, which can block.

    3rd) I believe it's a better idea to throw an exception is the constructor fails rather than return a failure. Your approach just says it "failed", as well, but not why, which is not very helpful.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    First I make it work, then I make it awesome
    Currently research OpenGL

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That said, though, it's usually not too helpful to go the "wrong way" too far since it will end up costing more time to make the "correct" changes.
    Like throwing an exception - just a simple one can't be too much work for the basic code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for this code? and 1 question....
    By Huskar in forum C Programming
    Replies: 6
    Last Post: 03-31-2009, 09:24 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. Some help with class member functions and pointers
    By k_rock923 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2005, 12:28 AM
  4. Architecture Question
    By Orborde in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2005, 08:05 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM