|
What you're looking for is some sort of factory class. My preferred method for registering class types to be created according to different types of string is to put a std::map into the factory that maps from strings to creator objects/function pointers. Something like: code:
code:
|
# ? Mar 25, 2009 16:30 |
|
|
# ? Jun 8, 2024 06:25 |
|
heeen posted:I want to make my classes scriptable, but I don't want to pollute everything with the script engine's implementation details. How do I handle callbacks with variable arguments nicely? Are you really committed to Javascript? If you can use Python, look into boost.python. It's really amazing how easily you can do "exotic" things like variadic member functions.
|
# ? Mar 25, 2009 16:51 |
|
Bitruder posted:BUT -- how does this work exactly? Can I still have a list of Neuron types? How can the library know what models are available? Should the models somehow "register" with the library? Yes, it should/can work. You want a base class that has all the functions things that deal with "Neurons" care about, some or most of which are virtual. (You can also make some abstract, which says to things; first, that you can never instantiate a Neuron, and second, that you can't instantiate a derived class unless it has it's own, non-abstract version of that function.) Then you make derived classes, say "interneuron", that has it's own (defined) versions of all the virtual and/or abstract functions. (This is literally the exact same declaration, minus the virtual and abstract identifiers) In your program, you then make a interneuron, and put it in a neuron slot. When the program goes "Okay, time to ask this neuron what it is...", it actually calls the interneuron's function, instead of the neuron's, due to the 'virtual'-ness of the function. What you can't do is have a slot for a neuron, and try to call a function from interneuron that isn't in neuron; there's no way to guarantee that that function exists for that object; all that's guaranteed is that it's (at least) a neuron. Example: code:
narfanator fucked around with this message at 20:13 on Mar 25, 2009 |
# ? Mar 25, 2009 20:07 |
|
ShardPhoenix posted:I can't figure out how to deal with 2D arrays. I have to make functions that can take and return 2D arrays of integers, but apparently functions in C++ can't return arrays as such, you have to return a pointer. This means that the functions have to take a pointer too, or they won't be composable in the way I need. However, I can't figure out how to actually do anything with an int**, like print it out. This is the code I have so far, but it segfaults whenever I try to dereference the pointer: I know you read the link the other guy posted and said it helped, but couldn't tell if you had actually found the solution to whatever you were doing. Anyway, I cooked this very small example up that shows some ways you might use int**. This is just test examples - you would want to keep proper track of things to keep in bounds, etc; you would also want to add in some error checking and cleanup, of course - I trust you can figure this part out. code:
|
# ? Mar 27, 2009 02:25 |
|
Zakalwe posted:Sort of a side note. In C++ you can pass references to arrays. Not many people seem to know this. Maybe a dumb question, but why would you do this? Arrays aren't copied when passed anyway, so what benefit does this serve?
|
# ? Mar 27, 2009 02:36 |
|
Luminous posted:Maybe a dumb question, but why would you do this? Arrays aren't copied when passed anyway, so what benefit does this serve? Knowing (and/or asserting) the By the way, for someone who claims to be helping someone understand the relationship between arrays and pointers, you don't seem to know much of what you're talking about, based on your code. (The first array subscript in a multi-dimensional array for function parameters always decays to a pointer. ROWS in a_block[ROWS][COLS] means exactly nothing). Avenging Dentist fucked around with this message at 02:44 on Mar 27, 2009 |
# ? Mar 27, 2009 02:39 |
|
Luminous posted:Maybe a dumb question, but why would you do this? To implement template<typename T, std::size_t N> std::size_t array_len(T(&)[N]) { return N; }
|
# ? Mar 27, 2009 02:42 |
|
^^ edit: thanks Van, that's exactly the sort of answer I was looking for.Avenging Dentist posted:Knowing (and/or asserting) the stride of a multi-dimensional array? I never made that claim at all. Feel free to improve - nobody else gave any examples. I am assuming you are not meaning any of the things I mentioned I was not going to include in the example. Also, no need to be snarky if you don't want to help. I asked a legitimate question to Zak (or anybody wanting to respond) - I wasn't being sarcastic to the guy at all. And your answer didn't clarify much (or your wording wasn't great). Why void bar(int (&foo)[100]) instead of just void var(int foo[100])? What specifically does the former give us that the latter does not?
|
# ? Mar 27, 2009 02:49 |
|
Avenging Dentist posted:Knowing (and/or asserting) the Are you trying to say the code I posted does not work, works by coincidence, or something else altogether? Because you're being exceptionally picky here if that is what you are focusing on. Yes, you can make the parameter a_block[][COLS]...what's your point? Is one more proper than the other (not a sarcastic question, see next sentence)? Now that you mention it, yeah, I can definitely see what I did as being misleading - but really, you could have said that without the 'tude, dude. Luminous fucked around with this message at 03:02 on Mar 27, 2009 |
# ? Mar 27, 2009 02:58 |
|
Luminous posted:
with int foo[100] foo is actually an int *. with int (&foo)[100] foo is a reference to an array. what does this mean? well for one thing the array carries things with it that a simple type* doesn't such as a size. sizeof(foo) will return 4 and 400 respectively (assuming a 32 bit machine)
|
# ? Mar 27, 2009 02:58 |
|
Zakalwe posted:with int foo[100] foo is actually an int *. Ahhh, that is concise. Again, exactly what I was looking for. I haven't used sizeof in a long long time. Shockingly, it is not the first thing to come to my mind. Thanks for the clarification
|
# ? Mar 27, 2009 03:01 |
|
code:
|
# ? Mar 27, 2009 03:05 |
|
Luminous posted:Are you trying to say the code I posted does not work, works by coincidence, or something else altogether? Because you're being exceptionally picky here if that is what you are focusing on. Yes, you can make the parameter a_block[][COLS]...what's your point? Is one more proper than the other (not a sarcastic question, see next sentence)? Now that you mention it, yeah, I can definitely see what I did as being misleading - but really, you could have said that without the 'tude, dude. Providing incorrect/misleading examples of array->pointer decay in C/C++ is worse than providing no examples at all. It's one of the most common things that cause confusion among new programmers. In a function argument list, code:
|
# ? Mar 27, 2009 03:13 |
|
Avenging Dentist posted:for instance, cplusplus.com actually asserts that arrays are pointers.
|
# ? Mar 27, 2009 03:29 |
|
Plorkyeran posted:I had so many people tell me that when I was learning C++ Color me uninformed. I have professors in school telling me to this day that arrays and pointers are one in the same. Anyone care to elaborate about how he is wrong on this one?
|
# ? Mar 27, 2009 05:09 |
|
Plorkyeran posted:I had so many people tell me that when I was learning C++ There are many cases where arrays can decay into pointers, but they are different types. Perfect example: code:
litghost fucked around with this message at 05:27 on Mar 27, 2009 |
# ? Mar 27, 2009 05:22 |
|
This is the most fundamental difference between arrays and pointers; if you understand why it is true both formally and as a matter of data layout, then you pretty much grok the subject.code:
|
# ? Mar 27, 2009 05:35 |
So, if you have something like char foo[3][3] is the data laid out by the compiler as identical to char foo[9]? i.e. foo[1][0] == foo[0][3]? I can't believe I've been writing C/C++ for this long without knowing this.
|
|
# ? Mar 27, 2009 07:38 |
|
huge sesh posted:So, if you have something like char foo[3][3] is the data laid out by the compiler as identical to char foo[9]? i.e. foo[1][0] == foo[0][3]? Yes.
|
# ? Mar 27, 2009 07:41 |
|
Can anyone point me in the direction of some resources on how to read from the serial port? If it matters, I think I'm going to do this in Linux because I have it in my head that it would be easier that way. I'm going to build a hardware random number generator that plugs into the serial port, so all I need to do is read bytes off the serial port, nothing fancy.
|
# ? Mar 27, 2009 14:08 |
|
dancavallaro posted:Can anyone point me in the direction of some resources on how to read from the serial port? If it matters, I think I'm going to do this in Linux because I have it in my head that it would be easier that way. I'm going to build a hardware random number generator that plugs into the serial port, so all I need to do is read bytes off the serial port, nothing fancy. *nix systems generally make devices directly accessible as a virtual file, so you should be able to fopen the appropriate /dev file and use the normal stdio.h functions.
|
# ? Mar 27, 2009 14:24 |
|
TSDK posted:Sure thing. Doh, you mean I can literally just fopen("/dev/tty_", 'r') and then read bytes from there? I didn't realize it would be *that* easy..
|
# ? Mar 27, 2009 14:26 |
|
dancavallaro posted:Doh, you mean I can literally just fopen("/dev/tty_", 'r') and then read bytes from there? I didn't realize it would be *that* easy.. http://www.comptechdoc.org/os/linux/programming/c/linux_pgcserial.html But basically it doesn't look all that tricky. EDIT: Using Win32 is pretty easy as well, since you just go through COM: http://www.codeproject.com/KB/system/SerialPortComm.aspx TSDK fucked around with this message at 14:48 on Mar 27, 2009 |
# ? Mar 27, 2009 14:32 |
|
But to test it I should probably be able to do something like "od /dev/tty" on the appropriate tty device file, right? Thanks for the help, I think those two links should be perfect.
|
# ? Mar 27, 2009 15:03 |
|
I'm not sure if this is the right place for this question, but I'm trying to do some very basic TCP and UDP communication in C++ in Windows, and I'm really not sure where to start. I'm having difficulty finding basic tutorials on Winsock stuff that I understand, so I'm wondering if there isn't a simpler way to do TCP and UDP communication. Any advice?
|
# ? Mar 27, 2009 16:15 |
|
Mach Won posted:I'm not sure if this is the right place for this question, but I'm trying to do some very basic TCP and UDP communication in C++ in Windows, and I'm really not sure where to start. http://www.codeproject.com/KB/IP/winsockintro01.aspx http://www.codeproject.com/KB/IP/winsockintro02.aspx
|
# ? Mar 27, 2009 16:24 |
|
Mach Won posted:I'm not sure if this is the right place for this question, but I'm trying to do some very basic TCP and UDP communication in C++ in Windows, and I'm really not sure where to start.
|
# ? Mar 27, 2009 17:08 |
|
We don't really have any restrictions on what we can use, but I have no experience with .Net. Winsock just seems like an awkward way to do all of this. All I'm doing is sending and receiving strings over TCP and UDP.
|
# ? Mar 27, 2009 17:40 |
|
Mach Won posted:We don't really have any restrictions on what we can use, but I have no experience with .Net.
|
# ? Mar 27, 2009 17:47 |
|
TSDK posted:It's like a dozen lines of very simple, very basic code - exactly how easy do you need it to be? I didn't mean that it was hard, just seemed messy compared to how I did TCP in MATLAB. Thanks for the links. I'm reading through them.
|
# ? Mar 27, 2009 18:35 |
|
Mach Won posted:I didn't mean that it was hard, just seemed messy compared to how I did TCP in MATLAB.
|
# ? Mar 27, 2009 18:57 |
|
boost.asio is pretty neat too and more C++ish than raw sockets. On the other hand, it is a lot more C++ish than raw sockets.
|
# ? Mar 27, 2009 19:26 |
|
Managed C++ is a really stupid thing to use unless you're porting legacy C++ code to the .NET platform. If you are starting anew, you might as well just use C# in that case.
|
# ? Mar 27, 2009 23:28 |
|
Edit: nvm..
NickPancakes fucked around with this message at 00:20 on Mar 28, 2009 |
# ? Mar 28, 2009 00:18 |
|
I posted last page about how to write a plugin-architecture and so far things are going alright. I've got it so that I have libraries that I load dynamically with dlopen() and I'm able to create objects, but here's the particular problem I'm having:code:
Thanks
|
# ? Mar 28, 2009 03:23 |
|
Your loaded libraries will need to register new plugin types with the central factory somehow, presumably during initialization.
|
# ? Mar 28, 2009 03:41 |
|
Could you show us the code for pluginfactory()? And what happens if you call initialize() directly?
|
# ? Mar 28, 2009 04:17 |
|
Here's the create() function in plugin factory that actually gives us the pointer.code:
code:
|
# ? Mar 28, 2009 13:07 |
|
I have some n strings to read from input I want to store, where n is known. I want to store pointers to these strings in an array, so I can loop over it and do things with them, for example print them to output. I wrote some test code; code:
|
# ? Mar 28, 2009 13:36 |
|
|
# ? Jun 8, 2024 06:25 |
|
DividesByZero posted:Why doesn't this work? My understanding is that scanf allocates memory and stores the read object at the pointer provided as the second argument. http://www.cplusplus.com/reference/clibrary/cstdio/scanf.html posted:The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.
|
# ? Mar 28, 2009 14:13 |