• Lucid Dreaming - Dream Views




    Page 3 of 5 FirstFirst 1 2 3 4 5 LastLast
    Results 51 to 75 of 102
    Like Tree3Likes

    Thread: Ask me about C / C++

    1. #51
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Quote Originally Posted by ninja9578 View Post
      Weird, huh?
      Yeah. That's not at all how I imagined they would work, but it seems fairly logical now that you present the idea. (Though I had to look spend some time just looking the code over and mentally executing it to get the picture :p)

      How would you include user defined functions? One way of doing it could be to just paste the code for each time it's used (like inline functions), but how would you get proper functions?
      Also, I'm guessing it would be pretty essential to implement a syntax checker?
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    2. #52
      Banned
      Join Date
      Jan 2004
      Gender
      Location
      nope
      Posts
      1,807
      Likes
      599
      How do I write a callback function that will get called at a rate that I can define?

    3. #53
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by khh View Post
      Yeah. That's not at all how I imagined they would work, but it seems fairly logical now that you present the idea. (Though I had to look spend some time just looking the code over and mentally executing it to get the picture :p)

      How would you include user defined functions? One way of doing it could be to just paste the code for each time it's used (like inline functions), but how would you get proper functions?
      Also, I'm guessing it would be pretty essential to implement a syntax checker?
      I actually don't implement a syntax checker, my company has a dev-staging-live process, my app is live so it's assumed that everything is correct when it gets to me.

      I do implement inline functions in that very way, and as for user defined real functions, well, my current language doesn't support those at this time, but I plan to add them in the very near future. They aren't that difficult. Basically, you would just replace a call to that function with something like @UserDefined(<place>) during a preprocessing pass. Then when you compile your callbacks, you have it point to the function that you've defined.

      Yeah, scripting languages are weird things, and difficult to start implementing, but once you've done a few of them, you can write pretty advanced ones really quickly.

      Quote Originally Posted by Artelis View Post
      How do I write a callback function that will get called at a rate that I can define?
      You mean in a scripting language, or in C++?
      Last edited by ninja9578; 02-05-2010 at 09:48 PM.

    4. #54
      Banned
      Join Date
      Jan 2004
      Gender
      Location
      nope
      Posts
      1,807
      Likes
      599
      Quote Originally Posted by ninja9578 View Post
      I actually don't implement a syntax checker, my company has a dev-staging-live process, my app is live so it's assumed that everything is correct when it gets to me.

      I do implement inline functions in that very way, and as for user defined real functions, well, my current language doesn't support those at this time, but I plan to add them in the very near future. They aren't that difficult. Basically, you would just replace a call to that function with something like @UserDefined(<place>) during a preprocessing pass. Then when you compile your callbacks, you have it point to the function that you've defined.

      Yeah, scripting languages are weird things, and difficult to start implementing, but once you've done a few of them, you can write pretty advanced ones really quickly.


      You mean in a scripting language, or in C++?
      in a scripting language...

      Isn't this a C/C++ thread?! Why would I be asking questions that AREN'T about C or C++? Well anyway, I'd prefer it in C, but I'm pretty sure it'd be handled the same way in either. Callbacks don't really use classes from my knowledge.

    5. #55
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Callbacks are just regular methods. All a method is is a pointer to a bit of code, nothing more, so you can store a pointer to a method just like to store a pointer to an object.

      Code:
      someclass * a = new someclass();
      someclass * b = a; // referencing a class
      Code:
      typedef int (*addcallback)(int, int);  //declare the type of callback and the parameters
      
      int ADD(int a, int b){  //the actual method which you will be calling back to.
        return a + b;
      }
      
      //in anywhere else in the same namespace
      addcallback add = ADD;  //make a reference to the method, just like you reference a class
      int j = add(4, 5);  //use the callback just like you would any other method
      What did you mean "that gets called at a rate that I define?" You can call the callback whenever you'd like.

    6. #56
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      function pointers are a good thing to get your head around for advanced stuff.
      Function pointers are at the heart of both polymorphism and function overloading
      you can do some seriously cool stuff with function pointers
      (\_ _/)
      (='.'=)
      (")_(")

    7. #57
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by Ynot View Post
      function pointers are a good thing to get your head around for advanced stuff.
      Function pointers are at the heart of both polymorphism and function overloading
      you can do some seriously cool stuff with function pointers
      Also shared libraries. Modularization is one of the most important things in advanced programming. No big program is one big program, it's a little program and a multitude of helper dlls or sos.

    8. #58
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      I'm looking for a cross-platform library implementing network connections, and a good guide on how to use it. If boost has such networking functionality that's be good.
      Basically what I want is a library which already implements the whole TCP/UDP. I'm just looking to create some communication between a generic client and a generic server. Know where I can find that?
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    9. #59
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, Boost has a network library. Boost Network I've never used it, but I know that it exists and because it's Boost there are lots of tutorials for it.

    10. #60
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      also have a look at QT
      it's much more than a GUI layer
      (in fact, you don't even have to use the GUI layer - you can just use the networking libraries)
      (\_ _/)
      (='.'=)
      (")_(")

    11. #61
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      wxWidgets too. I prefer wx because it's got a huge community for support, whereas Qt used to be a commercial product, so support is limited.

    12. #62
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      I changed my mind about wxWidgets. Don't use it, it's given me my last headache, I'm writing my own library from scratch, if anyone wants to help, let me know.

    13. #63
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Okai, I have a question... again :p

      So in a recent school assignment I need to dynamically allocate a 2D table. I chose to do it like this:
      Code:
      int** table;
      table = new int*[width];
      for (unsigned n = 0 ; n < width ; ++n) {
      	table[n] = new int[height];
      }
      (if there is a better way to do it, please tell me :p)

      But I had a problem when I was deallocating the memory again. I had thought I could simply do it like this:
      Code:
      for (unsigned n = 0 ; n < width ; ++n) {
      	delete[] table[n];
      }
      delete[] table;
      but that frequently (though not allways) caused the program to crash, citing heap corruption as the cause. So why would the above crash the program, and how can I deallocate the memory?

      (I realise the problem could be circumvented by allocating it as a 1D table, and write a function that can translate two coordinates to an index, but I want to know why the above doesn't work)
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    14. #64
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Well, there is a better way to do it, it's always best to allocate memory that goes together all at once. Remember, using a C-cast you can change anything into anything else.

      Code:
      int** array = (int**)malloc(width * height * sizeof(int));
      Then to delete it simply:
      Code:
      free(array);


      Looking at your code at a quick glance, I don't see anything incorrect. I would run it through the debugger and put a breakpoint on each delete[]

      Also, it's good practice to explicity declare what type you're using.
      Code:
      unsigned n = 0; //not standard C++
      unsigned int n = 0;  //correct

    15. #65
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Wow, quick reply

      Quote Originally Posted by ninja9578 View Post
      Well, there is a better way to do it, it's always best to allocate memory that goes together all at once. Remember, using a C-cast you can change anything into anything else.

      Code:
      int** array = (int**)malloc(width * height * sizeof(int));
      Then to delete it simply:
      Code:
      free(array);
      I have heard that it is good practise not to use the C functions in a C++ program, though I might have misinterpreted this. Would it be possible to use something like
      Code:
      int** array = static_cast<int**>(new int[width*height]);
      delete[] array;
      Also, how would that code know that I wanted a table of the dimentions width x height, and not height x width or something else entirely? I would want it to function identically to a static array that could be created with
      Code:
      int array[width][height];
      if width and height were constants.

      Quote Originally Posted by ninja9578 View Post
      Also, it's good practice to explicity declare what type you're using.
      Code:
      unsigned n = 0; //not standard C++
      unsigned int n = 0;  //correct
      Yeah, usually I define shorts like UINT in a header file. I did think that "unsigned" was standard C++ for "unsigned int", though, like "long" is standard for "long int". :p
      But since you have pointed out it isn't, I'll try and use fully qualified variable names from now on :p

      Quote Originally Posted by ninja9578 View Post
      Looking at your code at a quick glance, I don't see anything incorrect. I would run it through the debugger and put a breakpoint on each delete[]
      I just added a cout statement, and found that "delete[] table[0];" worked, and then it crashed on "delete[] table[1];" I tried having n count down from width-1 instead, but the result was the same, it crashed on the second deallocation.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    16. #66
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      No, using C functions is very common. C functions are very very fast. Don't use C headers. ie #include <ctime> instead of #include <time.h>

      Anyway, if I were you, I would write a structure to handle an array for you and a method to get specific items:

      Code:
      #include <iostream>
      
      struct Array {
      public:
          Array(unsigned int width, unsigned int height){
      	   w = width; h = height;
      	   array = (int*)malloc(width * height * sizeof(int));
          }
          ~Array(void){
      	   free(array);
          }
          inline int & at(unsigned int x, unsigned int y){
      	   assert(x < w);
      	   assert(y < h);
      	   return array[w * y + x];
          }
      private:
          int * array;
          unsigned int w, h;
      };
      
      int main(){
          Array test(100, 150);
          test.at(12, 43) = 15;
          std::cout << test.at(12, 43) << std::endl;
          return 0;
      }

    17. #67
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Oops. I forgot a const at operator

      Code:
          inline const int & at(unsigned int x, unsigned int y) const {
      	   assert(x < w);
      	   assert(y < h);
      	   return array[w * y + x];
          }
      Did you get how everything in my code works? This is how arrays are done behind the scenes anyway and because it's just a wrapper and the only functions are inline, the compiler will probably optimize away the structure.

    18. #68
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Mostly, yeah. But I don't get why you use assert. I thought the point of an assert statement was to halt program execution. Would it not be more suitable to use an if statement?

      Also, what does the const operators do in the last bit you posted? I have never learned to use them, except in variable declarations.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    19. #69
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, asserts halt operation, but only in debugging mode. In release mode, they are compiled out, so no checks are done. This helps you make your code correct before you release it.

      const at the end of a function means that object that it's being called on will not change. When variables are declared as const, it means that they can't be changed. So you can only call const functions.

      Code:
      #include <iostream>
      
      class foo {
      public:
          foo(void){};
          ~foo(void){};
          void Test(void) const {
      	   //not allowed to change anything in this class
      	   std::cout << "Test const" << std::endl;
      	   //bar = 1;  <- This is illegal
          }
          void Test(void){
      	   //can manipulate the object
      	   std::cout << "Test" << std::endl;
      	   bar = 1; //This is fine
          }
      private:
          int bar;
      };
      
      int main(){
          foo one;
          one.Test();  //foo is not const, so the non-const method will be called
          const foo two;
          two.Test();  //foo is const, so the const method is called
          
          return 0;
      }

    20. #70
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      const essentially means "read only" so
      operator [] lets you read and write to the class
      operator [] const lets you read from the class only

    21. #71
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Okai, I understand. Thanks
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    22. #72
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      I thought I should pop up a slightly old thread for this problem I'm having.

      I started *trying* to use wxWidgets.
      IT makes sense... a little bit.

      But there is one roadblock in my way of progressing in a program I am trying to get done.

      Basically, I want to be able to click a button (In this case, "Button2"), so that the text changes. I can do that fine. But I then want to be able to click it again so that it changes to something new.


      I have tried countless different possible ways of what I thought would work, but I just can't get it.

      This was my last attempt (The bit I'm at a problem with):

      Code:
      void Physics_StudyFrame::OnButton2Click(wxCommandEvent& event)
      {
          for (int a=0; a<2; a++)
          {
              (StaticText2)->SetLabel(_("FirstClick new Text"));
              a = a + 1;
          }
      
      }
      
      
      void Physics_StudyFrame::OnButton2Click2(wxCommandEvent& event)
      {
          (StaticText2)->SetLabel(_("Second Click New Text"));
      }

      I dunno, In my head it seems that should work. But it just doesn't.

      I thought about possibly making the button disappear on first click and then a new one re-appear, so that for the second click I could just use the new button. But... I don't even know where to begin for that one

      Halp

    23. #73
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      Quote Originally Posted by slash112 View Post
      Code:
      void Physics_StudyFrame::OnButton2Click(wxCommandEvent& event)
      {
          for (int a=0; a<2; a++)
          {
              (StaticText2)->SetLabel(_("FirstClick new Text"));
              a = a + 1;
          }
      
      }
      I didn't look at any of your code, but this popped up immediately: why are you incrementing a twice? Once as part of the for loop (a++), and once at the end (a = a+1). while I have no idea what you're trying to do, I'd be shocked if this weren't seriously broken.

    24. #74
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      My guess is that you are using an event table for this, I would recommend connecting your events and disconnecting them manually.

      When you create it, do a ConnectEvent, on the first click, do a DisconnectEvent, then ConnectEvent to have it go to the new method.

    25. #75
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Quote Originally Posted by Replicon View Post
      I didn't look at any of your code, but this popped up immediately: why are you incrementing a twice? Once as part of the for loop (a++), and once at the end (a = a+1). while I have no idea what you're trying to do, I'd be shocked if this weren't seriously broken.
      A series of stupified attempts brought me to this equally stupid one.
      Basically, I thought that I could use a loop, to make it sort of stop... or something. I dunno, it seems more stupid when I start thinking about it more...

      Quote Originally Posted by ninja9578 View Post
      My guess is that you are using an event table for this, I would recommend connecting your events and disconnecting them manually.

      When you create it, do a ConnectEvent, on the first click, do a DisconnectEvent, then ConnectEvent to have it go to the new method.
      Ahh, well this makes sense, actually.
      *Goes to Google*

    Page 3 of 5 FirstFirst 1 2 3 4 5 LastLast

    Bookmarks

    Posting Permissions

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