|
Smackbilly posted:Tbe biggest advantage is that enums are typesafe, so you will not need that extra "assert" statement to check that the parameter is in the valid range. If you have: room->link(0, Direction(14));
|
# ? Jun 5, 2008 12:47 |
|
|
# ? Apr 28, 2024 20:14 |
|
Vanadium posted:room->link(0, Direction(14)); Well there what you're doing is performing a type-unsafe C-style cast. If you go around doing that, all the type-safety in the world isn't going to help you, since you're basically telling the compiler "I know better than you do here; shut up". And if you actually don't know better... By the way, regarding the "counting the valid enums" problem, here's how I usually handle it: code:
Smackbilly fucked around with this message at 13:04 on Jun 5, 2008 |
# ? Jun 5, 2008 13:00 |
|
Why do you use a struct declaration instead of a namespace?
|
# ? Jun 5, 2008 13:15 |
|
floWenoL posted:On the other hand, it introduces an invalid value (kNumWhatever) that is treated like a valid value. Plus GCC on overly paranoid settings will complain if you do this: code:
But you should be putting default: blocks in switch anyway.
|
# ? Jun 5, 2008 17:00 |
|
I haven't tested it thoroughly, but I just put together a little macro for simple, safe enums if anyone wants it. You need boost installed to use it (header only). I only compiled with VC++ 9 but it should be portable unless I made a poopie, which is probably pretty likely. Right now, enums must be created at namespace scope, but with some effort, that can be changed. To make a simple direction type that has north, south, east, and west constants and that default constructs to north, use: code:
code:
code:
code:
code:
code:
That Turkey Story fucked around with this message at 01:59 on Jun 6, 2008 |
# ? Jun 5, 2008 18:04 |
|
Mustach posted:Why do you use a struct declaration instead of a namespace? Because not everyone programs in C++.
|
# ? Jun 6, 2008 01:59 |
|
floWenoL posted:
Actually the real reason is because you can't declare a namespace inside a class. So if I want to do something like this: code:
Smackbilly fucked around with this message at 16:26 on Jun 6, 2008 |
# ? Jun 6, 2008 13:01 |
|
That makes sense, thanks.floWenoL posted:Because not everyone programs in C++. code:
|
# ? Jun 6, 2008 14:26 |
|
Also you cannot have a static const NUMDIRECTIONS field in C.
|
# ? Jun 6, 2008 14:28 |
|
This is more a gcc question, but: Is there a way to force gcc to link in everything from a static library, even if you're not using it? The context is that I want to create a shared library libfoo.so which uses libbar.a, and I want EVERYTHING in libbar.a to be available to programs which link against libfoo.so without having to have libbar available. The problem is that there can be programs which use symbols in libbar.a which are not used by libfoo.so, so by default these symbols are not included in libfoo.so when it statically links against libbar.a.
|
# ? Jun 6, 2008 16:35 |
|
Smackbilly posted:This is more a gcc question, but: gcc is really just a front to some other utilities. The one that controls linking is "ld", and the option you need to pass to it is "-E", I think (source). You can use "-Xlinker option" with gcc to pass the flag to the linker (source), or call ld yourself, passing in the object files to make into a .so. There is probably a direct gcc flag, but I didn't see it.
|
# ? Jun 6, 2008 18:17 |
|
Mustach posted:That makes sense, thanks. You're absolutely right; I don't know what I was thinking.
|
# ? Jun 7, 2008 09:29 |
|
What's the most elegant way for a class to expose a collection? At the very least, it needs add, remove, and enumeration faculties. Preferably it shouldn't be tied to any particular container type, either. I've run up against this pattern a few times and haven't found any particularly elegant solution.
|
# ? Jun 8, 2008 10:38 |
|
Paniolo posted:What's the most elegant way for a class to expose a collection? At the very least, it needs add, remove, and enumeration faculties. Preferably it shouldn't be tied to any particular container type, either. I've run up against this pattern a few times and haven't found any particularly elegant solution. Could you elaborate more on your situation? Before you decide to directly expose the container, make sure you can't just represent your functionality as higher-level operations that are internally implemented with a container. If the encapsulating class holds the container but all the manipulation is still done directly by users rather than indirectly through the class, then your type is likely just a low-level aggregate (which is okay depending on your situation, think std::pair or tuple types) or it is a container itself (which again is okay). In the case of it being an aggregate, you can always just directly expose your object rather than making a redundant interface. If it's a container itself, the easiest way to make your type a compliant container without an is-a relationship is to privately inherit from the container you are using and then promote the interface elements to public with using. From your brief description, it sounds to me like you are in more of the latter case. If you feel as though your type is neither of these things, then try to tell more details about what you are doing.
|
# ? Jun 8, 2008 17:00 |
|
I decided I'd try goofing a bit of GDI tonight, so I decided to make a starfield. It pretty much works, except I don't know how to repaint the window. If I drag it all over, it updates fine, which is basically the effect I need, because right now it's just moving the stars but since it's not redrawing anything, I'm just getting streaks. RedrawWindow sounded right enough, but apparently I'm stupid and it's not doing anything. code:
|
# ? Jun 9, 2008 10:20 |
|
Look up InvalidateRect.
|
# ? Jun 9, 2008 13:19 |
|
tyrelhill posted:Look up InvalidateRect. Thanks.
|
# ? Jun 9, 2008 18:23 |
|
As a project for a systems class, I need to write a sequential-logging proxy in C. But I can't manage to get a piece of code to submit a valid HTML request. The following just hangs for a second, and prints a blank string. The same request submitted using telnet gives me google's home page. What am I doing wrong? (The Rio stuff is a set of robust/buffered IO functions we've been given to use for the class. The professor has used them before, so I'm going to guess they're not the issue. Rio_readn reads n characters, Rio_readinitb sets up a buffer for an rio struct / file descriptor pair, and Rio_readlineb reads lines from the buffer, refilling the buffer as it goes.) code:
|
# ? Jun 10, 2008 18:03 |
|
Shoes posted:What am I doing wrong? HTTP requests need to end with a blank line (because there can be multiple lines in a single request). Stick another \r\n on the end and you should be fine.
|
# ? Jun 10, 2008 18:09 |
|
Smackbilly posted:HTTP requests need to end with a blank line (because there can be multiple lines in a single request). Stick another \r\n on the end and you should be fine. D'oh! Thanks!
|
# ? Jun 10, 2008 18:10 |
|
Another GDI question. The more I use it, the more I notice that SetPixel is slow as hell. What's a faster way to draw?
|
# ? Jun 11, 2008 18:03 |
|
slovach posted:Another GDI question. What are you drawing? Any primitive draw function (line, circle, etc) or (ideally) bitmap blitting is going to be a lot faster overall, due to hardware acceleration.
|
# ? Jun 11, 2008 18:52 |
|
Nuke Mexico posted:What are you drawing? Any primitive draw function (line, circle, etc) or (ideally) bitmap blitting is going to be a lot faster overall, due to hardware acceleration. Thanks, I started using a DIB and speed gain is enormous, probably well over 10x in extreme cases. A little confusing since I basically had to make my own equivalent of SetPixel, but well worth it.
|
# ? Jun 12, 2008 02:38 |
|
While reading through the Qt documentation the other day, I noticed that they use a simple strategy to get around the include file mess that we talked about that in here a few months ago. I don't remember seeing it mentioned when we talked about this, and it seems like a pretty nice solution.code:
code:
code:
ColdPie fucked around with this message at 05:12 on Jun 12, 2008 |
# ? Jun 12, 2008 05:09 |
|
It's called a forward declaration. It's used all over the place -- it's the standard way of removing the include file mess. Note that it only works on pointers or references, where the compiler doesn't need to know the full definition of the object. So: code:
code:
[edit] FYI forward declaring classes/structs in headers saves a huge amount of compile time in large code bases. Seeing code that includes unnecessary headers in other headers makes me seethe with rage. Also, if what you're trying to forward declare is inside a namespace, you can do: code:
cronio fucked around with this message at 05:16 on Jun 12, 2008 |
# ? Jun 12, 2008 05:12 |
|
Forward declarations will only work for pointers or references, where the compiler doesn't need to figure out the size of the type. In your "Another" class, it will have to contain either a pointer or reference to SomeClass. It's a pretty common usage pattern
|
# ? Jun 12, 2008 05:14 |
|
I see, thanks for the tip. Hopefully someone in here hasn't seen it before and thinks it's as awesome as I do
|
# ? Jun 12, 2008 05:15 |
|
For reference, it's commonly called the 'pimpl idiom' in C++, and more generally known as an 'opaque pointer': http://en.wikipedia.org/wiki/Opaque_pointer
|
# ? Jun 12, 2008 08:26 |
|
I'm trying to run a relay off a serial port on windows using code I found here (not the one included in the article, the one found about 1/4 of the way down the page) when I try to compile it a get a whole bunch of "'WORDS' undeclared (first use in this function)" errors My programming experience comes entirely from a university course I took a couple years ago where we made C programs to do basic calculations, read/write text files and not much else...so I'm a little over my head dealing with this kind of stuff What do I need to do to make this work?
|
# ? Jun 12, 2008 15:50 |
|
TheBlackRoija posted:when I try to compile it a get a whole bunch of "'WORDS' undeclared (first use in this function)" errors Please post the exact error messages.
|
# ? Jun 12, 2008 16:19 |
|
TheBlackRoija posted:when I try to compile it a get a whole bunch of "'WORDS' undeclared (first use in this function)" errors Tell us what compiler you're using, what OS (and version) you're compiling on, and copy-paste the exact errors you're getting.
|
# ? Jun 12, 2008 16:19 |
|
cronio posted:It's called a forward declaration. It's used all over the place -- it's the standard way of removing the include file mess. This is true for aggregation of objects in classes, but interestingly you can use forward declaration to define function parameters that you intend to pass by copy (you don't need to make them references or pointers): code:
|
# ? Jun 12, 2008 16:34 |
|
I'm having an issue with a program I'm writing in C that's supposed to talk to our database servers for periodic status updates. Here's the offending code section that I'm having problems with:code:
The problem that I'm having seems to be a problem with allocating memory. sizeof(active_query_t) returns 80, which seems too small, since this is the definition of the struct: code:
Whatever's going on, it's causing real problems for me. If I malloc() the memory (as in the code example above), the code always crashes when trying to set curr_q->bs_time_t. If I use g_slice_alloc() instead, the code crashes even earlier when setting the value of curr_q->bs_tm in strptime(). I'm not really sure where to begin with this, since I don't see any obvious errors in my handling of allocating memory. I tried allocating 4*sizeof(active_query_t) just to see if that would help, but it continues to crash. None of the code before the time handling code crashes, but it will either crash in there or in get_hash if I comment out the time handling code. Setting curr_q->notified never crashes. EDIT: this is on Linux (Debian unstable AMD64) with gcc 4.3. chutwig fucked around with this message at 17:06 on Jun 12, 2008 |
# ? Jun 12, 2008 17:04 |
|
chutwig posted:The problem that I'm having seems to be a problem with allocating memory. sizeof(active_query_t) returns 80, which seems too small, since this is the definition of the struct: I'd recommend reading chapter 5 of K&R for starters. EDIT: Your current crash is because bs_tm is an uninitialsed pointer to a random bit of memory that you're then trying to write to, but if you don't understand pointers then you're going to run into yet more problems real soon. TSDK fucked around with this message at 17:35 on Jun 12, 2008 |
# ? Jun 12, 2008 17:31 |
|
csammis posted:Tell us what compiler you're using, what OS (and version) you're compiling on, and copy-paste the exact errors you're getting. Nevermind, I'm just retarded, no <>'s around the filenames... ..and I changed "void main" to "int main" with the return 0 at the end whether or not the program is run I get -10V across the pins and I should be getting +10 after.. Anyone have experience running circuits off of serial ports? I'm using Dev-C++ and running XP btw now I'm just wondering if there is a way I can trigger this off an existing program, either by closing itself or by breaking an infinite loop...or any other way I don't have the knowhow to have thought of. The program does 2 things when this one should be triggered, transmits data through the serial port and plays a sound... TheBlackRoija fucked around with this message at 20:56 on Jun 12, 2008 |
# ? Jun 12, 2008 18:30 |
|
TSDK posted:current_query et al are pointers to GString objects, likewise bs_tm is a pointer to a tm struct. Okay, I understand my first mistake; forgetting that the pointer is its own type with a different size from what it points to. That being said, I guess I need to issue a malloc for bs_tm and then have it write to that, and probably also malloc the other pointers before I write to them. I know I don't completely understand pointers yet; that's why I'm undertaking this now, so that I become more aware of manual memory management rather than simply letting the computer always do it for me. Just telling somebody to read K&R again doesn't help much when dealing with something like pointers that is very complex. chutwig fucked around with this message at 18:41 on Jun 12, 2008 |
# ? Jun 12, 2008 18:39 |
|
chutwig posted:Okay, I understand my first mistake; forgetting that the pointer is its own type with a different size from what it points to. Yeah, you're getting confused between the pointer's value versus the value of what it's pointing at. The pointer just contains an address (4 bytes worth) of a location in memory. That location in memory is hopefully something useful (a variable, for instance). Remember that the pointer is not the value of that variable, it's just a reference to it. You can change what it's pointing at at any time. For example: code:
|
# ? Jun 12, 2008 20:47 |
|
quote:The pointer just contains an address (4 bytes worth) of a location in memory. The size of a pointer is architecture-dependent. Under 32-bit, a pointer is 4 bytes; on 64-bit, a given memory location is 8 bytes long.
|
# ? Jun 12, 2008 21:16 |
|
Mikey-San posted:The size of a pointer is architecture-dependent. Under 32-bit, a pointer is 4 bytes; on 64-bit, a given memory location is 8 bytes long. True!
|
# ? Jun 12, 2008 21:18 |
|
|
# ? Apr 28, 2024 20:14 |
|
chutwig posted:Okay, I understand my first mistake; forgetting that the pointer is its own type with a different size from what it points to. The biggest problem people generally have with learning pointers is that they're told "oh yeah, pointers are hard, that's why I hate C," and then they believe it's some horrible convoluted thing.
|
# ? Jun 13, 2008 00:58 |