|
Here is some sample code: http://codepad.org/TOTixB1a Same code is pasted here if you don't want to click: code:
CircleInterface extends ShapeInterface, and CircleImpl extends ShapeImpl (while also implementing CircleInterface). The error that the compiler throws is: cannot declare variable 'circle' to be of abstract type 'CircleImpl'. My problem is with the 2 SetName/GetName methods. They are defined in ShapeInterface, and implemented in ShapeImpl. However, it seems like I have to implement them in CircleImpl as well (even if they are just calling ShapeImpl::SetName()/ShapeImpl::GetName()). Am I doing something wrong, or this is the way it's supposed to happen in C++. Coming from Java, this construct (extend A implement B) is perfectly valid and I do not have to implement any methods that A defines that are implemented in B. Can it be done in C++ too? Or will I just have to live with it?
|
# ¿ Jun 3, 2010 02:22 |
|
|
# ¿ May 2, 2024 15:18 |
|
Avenging Dentist posted:Use virtual inheritance: http://codepad.org/Q21fhVP1 Thank you sir.
|
# ¿ Jun 3, 2010 02:32 |
|
From what I can see, you appear to be using these in an awkward way. Normally the code would look like this:C++ code:
If you want to do this from both sides, use 2 semaphores. One thread only calls Wait() on semaphore1 and signals on semaphore2 and the other thread signals semaphore1 and waits on semaphore2. Of course, the fun part comes when both threads wait, and neither can signal each other .
|
# ¿ Jan 25, 2013 04:38 |
|
Boz0r posted:I'm supposed to use SIMD and SSE to to optimize runtime of a raytrace renderer. I can't seem to find much info on how to use this in Visual Studio. Does anyone have any good resources for this? Are you referring to this http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions ? Because these are just ... instructions. You can write them by hand or use a library. Maybe this would be helpful: http://stackoverflow.com/questions/61341/is-there-a-way-to-insert-assembly-code-into-c
|
# ¿ Feb 5, 2013 23:40 |
|
Vanadium posted:This seems really complicated to get right, I think I am going to jump ship into the -fno-rtti -fno-exceptions camp. I found this boostcon video to be extremely educative: https://www.youtube.com/watch?v=N9bR0ztmmEQ Personally I believe that writing exception-safe code is not that hard, and that it is worth it. There are way too few cases where disabling exceptions makes sense, and too many benefits of using them.
|
# ¿ Mar 29, 2013 00:36 |
|
Has anyone ever used libunrar (from https://github.com/vilkov/libunrar)? I have the following code: code:
code:
Now, when I call this function with a simple rar file everything is fine and the file is extracted in the specified directory. When I call this function on a multi-volume archive, I get something like this: pre:Cannot extract /tmp/test/test.r00 Cannot extract /tmp/test/test.r01 Cannot extract /tmp/test/test.r02 Cannot extract /tmp/test/test.r03 Successfully extracted /tmp/test/test.rar Thank you. Edit: Im using gcc (GCC) 4.8.1 20130603 (Red Hat 4.8.1-1) on Fedora 19 if that matters. Volguus fucked around with this message at 02:17 on Aug 13, 2013 |
# ¿ Aug 13, 2013 02:07 |
|
There are a lot of open source programs out there (7z for example) that can and do uncompress rar files. Won't create them, but can extract them. I'll go study them to see how do they do it, but I was kinda hoping for some obvious mistake that I'm doing that will let me extract multi volume archives without too much fuss. The unrar binary is quite convoluted.
|
# ¿ Aug 13, 2013 13:14 |
|
Gazpacho posted:Why do you want to check the byte order? If you need to encode and decode integers in a standard format, Unix systems already have functions for that. In embedded development you may. If you get a board from some strange vendor that has a chip with few K of ram and runs like is being powered by hungry hamsters, chances are that you won't have access to endian.h . At which point you will have to write your own test routine. What I found on stackoverflow looks like a good approach, though there may be simpler ones out there: http://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine
|
# ¿ Sep 30, 2013 22:09 |
|
Here's a talk from Alexandrescu where he shows a manual vtable implementation in one of the slides, which apparently is faster than the gcc generated one (the entire talk is very interesting in and of itself): http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly?video
|
# ¿ Oct 11, 2013 14:27 |
|
pr0metheus posted:What is the best resource for studying C++11 features? I am familiar with older specification (C++03), but need to big up on the new things. Don't know if it's the best one, but this is a quite nice cheat sheet: http://isocpp.org/blog/2012/12/c11-a-cheat-sheet-alex-sinyakov
|
# ¿ Oct 12, 2013 02:09 |
|
more like dICK posted:AI've got an class that owns a std::vector<Foo>. A Foo can be in one of two states, up or down. Foos are big and don't like being copied. Clients of this class only care about getting a sequence of Foos that are up, so I'd like to provide a view over the vector. Is something like a forward_list<reference_wrapper<Foo>> the best way to provide this? Assuming here that the class will add and remove Foo references to the list as they change state. Does a list of reference_wrappers offer any advantage over a list of pointers? shared_ptr's could help you here.
|
# ¿ Oct 26, 2013 03:48 |
|
Rusty Kettle posted:I have a bunch of programs that generate x,y,z coordinates in an iterative way for an infinite amount of time, and a program that displays the x,y,z coordinates in a pretty visualization. Another way could be using actors. libcppa is one library that provides an actor system implementation.
|
# ¿ Nov 26, 2013 01:24 |
|
bobua posted:Idea check: Why not look at active objects? It looks like it's the thing you're looking for: http://herbsutter.com/2010/07/12/effective-concurrency-prefer-using-active-objects-instead-of-naked-threads/ For the message queue i used a simple sync queue implementation. You can probably find better ones: http://pastebin.com/ghVeC68f Volguus fucked around with this message at 03:59 on Dec 3, 2013 |
# ¿ Dec 3, 2013 03:52 |
|
Dren posted:I can't seem to find the project settings I need in KDevelop. It couldn't find the includes it needed so it prompted me to edit the custom include path, which I did, but now I can't get back to that menu. The best thing about KDevelop is that you don't need project settings. Everything you need is in the CMakeLists.txt file. You add include folders there, linking libraries, etc.
|
# ¿ Dec 11, 2013 02:07 |
|
Tetraptous posted:Interesting! So does this mean that if I have an existing CMake project, I can just point KDevelop at it and it'll just work? Yes, it should "just work". As for Emacs mode, i don't know if it supports that (or what exactly this mode would mean to you), but you can configure the shortcuts. Another decent IDE is QT Creator (which also supports CMake files, among other things). Version 2.8.1 found in Fedora 20 is actually surprisingly nice. I tried it some few years back and it sucked, but now i am actually using it for a week and all is fine and dandy.
|
# ¿ Dec 11, 2013 05:19 |
|
For C# to native C++ you would have to go through C++/CLI (the .net c++). That is, you would have your c# code, that gets the file that has been opened, which would call a method in c++/cli with the .net string, which will convert that .net string to std::string (or const char* or whatever) which will call your library. No idea about Obj-C but something can be done in there as well. If you want to go web app route, you can use an already made framework such as cppcms, which will allow you to run locally, natively, and be a web server at the same time. And apparently it can be compiled to work on all the platforms (and you solved the cross-platform ui problem).
|
# ¿ Dec 23, 2013 20:35 |
|
movax posted:Ah, client-side JS/HTML5? (I am not a web-app guy at all) So the source would be exposed on their side right? On the other hand, what would the js/html5 code do? render certain things. the guts of the problem could still be hidden away on the server side (native c++ app). unless of course, the entire thingy is just the view, in which case minify, minify, minify. 3 times the charm.
|
# ¿ Dec 24, 2013 05:46 |
|
Rocko Bonaparte posted:So assuming I have a shared pointer as a member, in the initialization it would be... my_shared_ptr(make_shared<bla>(whatever))? std::shared_ptr<bla> my_shared_ptr = std::make_shared<bla>(params);
|
# ¿ Feb 7, 2014 02:25 |
|
Subjunctive posted:Well Andrei obviously does. Andrei just bragged not too long ago about pushing the first D program in production at facebook: http://forum.dlang.org/thread/l37h5s$2gd8$1@digitalmars.com Volguus fucked around with this message at 23:06 on Mar 28, 2014 |
# ¿ Mar 28, 2014 23:03 |
|
You were using exception for control flow. Don't do that. It is a well known anti-pattern. Exceptions should be used for exceptional situations, not to break from a loop. Essentially, this usage is no better than a goto. Here's a stackexchange thread that may be more helpful: http://programmers.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why
|
# ¿ Jun 6, 2014 20:17 |
|
Plorkyeran posted:That you're using std::stoi is very relevant, and makes that code reasonable IMO specifically because you're reusing the error handling that you need anyway, unlike if it was std::strtol where the throw and catch would be pointlessly roundabout. std:stoi throws std::out_of_range if the value falls out of the range of the type. Here maxAllowedValue is the business rule regarding that type. There are two very different things. Yes he needs that try/catch, but the catch is only for when the user enters a value that cannot be represented by an int. For when the user enters a reasonable int value, but that violates the business rules (maxAllowedValue) he should either throw his own exception so that somebody up the chain can handle that (show the user the error message for example) or just return like he does in his out_of_range catch. Throwing an std::out_of_range because the user entered value 20 (if maxAllowedValue is 15 for example) is just control flow. It shouldn't be a habit to catch on.
|
# ¿ Jun 6, 2014 20:43 |
|
Plorkyeran posted:I get that much, and I can buy the argument that using std::out_of_range for business logic violations is incorrect because it's for precondition violations, but I don't understand how throwing one type of exception is using exceptions for control flow but throwing another type isn't (or is acceptable control flow). Because you wouldn't (shoudlnt) catch your exception in the try/catch. That one is meant for whoever is calling you. In my opinion, the code would be better with something like this: code:
The cause of that exception is not really relevant to the caller, so it shouldn't be burdened with that. Why is this better than just throwing std::out_of_range? Because tomorrow you may change the code, to convert a string to int using some other algorithm. One that doesn't throw std::out_of_range. Or maybe you will do some other black magic inside your function. By only telling your caller that the only thing he has to worry about is invalid_output_exception, you are now free to do whatever you want in your function, use whatever algorithm you want and just make sure that you throw that exception if you dont like the input (for whatever reason). A change in the business rules should only be your problem, not the caller's.
|
# ¿ Jun 6, 2014 23:16 |
|
Plorkyeran posted:The fact that some std::out_of_range are coming directly from your function and some happen to be bubbling out is an implementation detail that the caller does not need to be aware of. If you need to change to a different string parsing method that signals errors in a different way, then you just have to throw std::out_of_range whenever the new string parsing method signals that the value is out of range. Choosing an interface which simplifies your implementation does not mean that your interface has to change to keep your implementation simple, and in this case your implementation after changing would be identical to what it would have been if you prematurely pessimized by translating std::out_of_range (other than the name of the exception being thrown, obviously). But if the business rules change so that the input is invalid for some other reason (mars, earth and the sun are in alignment for example) then throwing out_of_range is kinda ... wrong, isn't it? The input is invalid. Why? Why do you care? It's invalid and that's that. The input doesn't have to be "out of range" to be invalid, that's my entire point. Hiding that implementation detail from the caller is, in my opinion, good design practice. Then again, probably in most real world cases this may not matter, no matter what you throw out (if anything, just returning may be just fine). Of course, in cases where good design practices have a performance penalty ... then screw good design. But usually, it pays in the long run.
|
# ¿ Jun 6, 2014 23:44 |
|
Plorkyeran posted:The obvious case is that you want to show different error messages for different problems, and having the error message controlled by the UI rather than the layer throwing the error. It's much easier to throw away excess detail about what exactly went wrong than to go the other direction. Wanting all of your validation exceptions to have a common base class to simplify that would be a good reason for mapping std::out_of_range to a different exception type, but I don't have any idea what that has to do with the idea that throwing std::out_of_range is using exceptions for control flow and throwing some other exception is not. It all has to do with how it's handled. Throwing out_of_range from your function exposes an implementation detail (may or may not be ok, usually is not). Throwing exception within a try and catching it later (rethrowing it or just returning doesnt matter) it is just a simple control flow problem. Why? You can use a simple return if you want to exit the function cleanly. You can use a boolean variable if you want to check for success later. You have 100 other mechanisms available to accomplish the same thing without going into exceptions. There are quite a few examples given at http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl . However, in C++ land, one must be worried about performance as well: https://www.youtube.com/watch?v=N9bR0ztmmEQ. The links provided hopefully can explain better than I can. If not, don't worry, for most cases it's probably not a huge issue.
|
# ¿ Jun 7, 2014 05:43 |
|
Hammerite posted:I feel like this argument is kind of going over my head. I don't really understand the whole discussion about passing information about failure back to the caller. As I said to astr0man on the previous page, the point of the function in which the relevant code appeared (at least, the point of the function as I originally intended it) was that it would test the user's input, decide whether it was valid, and either display a "friendly" error message explaining what was wrong (if it was not valid) or execute the requested operation by delegating to other functions (it it was valid). By design, the main loop of the program doesn't need to know which of those it was. It just repeatedly delivers lines of input to this function, it doesn't care what they mean or whether they worked. In which case then, a simple return from the if statement would do, just like you're doing from the catch.
|
# ¿ Jun 7, 2014 16:41 |
|
Optimus Prime Ribs posted:The problem was what MutantBlue pointed out (i.e. me accidentally starting the thread twice), but what difference would putting the function's definition below main make? Wouldn't the compiled program be the exact same? It would. For 100 lines programs there is no difference in readability/maintainability regardless of the coding practices employed. Variable names as a,b and c are perfectly fine. goto's all over the place are perfectly fine. However, after you've been working for a while on very large projects, you start to realize that readability is the most important part. Everything else is a distant second. Optimizations, bug fixing, adding new features become trivial (usually) if the code itself is readable, follows conventions, is essentially clean. Saying "But i will follow the rules once i get a job" doesn't usually work. You will have to meet deadlines, you will work under pressure. If the habit is not formed to keep the code clean by then, you will have a hard time adjusting.
|
# ¿ Jul 11, 2014 16:24 |
|
From the looks of it, something like a key/value database would help. Have you looked at Redis? Or MongoDB (though that's a document store, but it may be just what you need)? Both have C apis. I am not 100% sure about querying though.
|
# ¿ Aug 3, 2014 00:15 |
|
FateFree posted:I am successfully using this code to execute a hash. However my next step is trying to return the result to a java application via JNI. The method signature looks like this, but I can't figure out how to convert the output array to a jbytearray. Here is what I tried after seeing some examples: First of all, there are plenty of pure java libraries out there that can calculate sha-256 from a string. But, for the sake of argument, let's assume that you have to (somehow) use JNI, and that function specifically (sha256_hash). Apparently, that function is giving you a uint32_t array. Now, each element of that array is (duh) a uint32_t, that is an unsigned 4 bytes integer. You cannot (normally) convert that to a byte array, since each byte is , well, 1 byte, not 4 (well, you can cast it if you really want, but then you're endianess dependent, it's more headache than it's worth. plus ... who's gonna delete it? The Java gc won't know about it). You cannot even use a java int, since int in java is a 32-bit signed two's complement integer. Long would be the least you can do. And you most likely cannot just write memset, but instead go through each element of the array and assign the value to the other array (longArray[i] = output[i]). Or, maybe memcpy could work somehow, but it such a freaking pain. My advice would be to just go to a java library that does that for you. If you're worried about performance, then Java is not the right language to write your program in. Use plain C or C++.
|
# ¿ Sep 21, 2014 05:39 |
|
FateFree posted:Thanks for the insight. I'm aware of the java libraries but this is more of a JNI exercise that I can verify against the java libraries. But I am also trying to maximize performance. I think just to get it working I'll pass back a hex string to java and then i won't need to worry about these data types. Qwerty gave me a print loop which displays the correct hex, so I'd like to try to append that to a string and return it as the result of the method. Can I utilize the printf function somehow, since it seems so compact? Here is what he gave me: You could use sprintf on a char array, and put that into a string. Something like: code:
|
# ¿ Sep 21, 2014 17:39 |
|
epalm posted:Here's probably a stupid C++ question! std::stringstream.str(buf) should work. But, ensure that you initialize buf to all 0's before doing the copy. Or make a std::string from that buf, where you can specify the length: code:
|
# ¿ Sep 26, 2014 20:47 |
|
Newf posted:I'm looking to drop a real simple webserver into a windows application - it only has to respond to GET requests and will have to serve a page that it generates from some of its own internal state. Microsoft has made an open source C++ REST SDK that may be able to help: https://casablanca.codeplex.com/
|
# ¿ Jan 23, 2015 14:38 |
|
Diametunim posted://stuff Since i can't sleep, i came up with this inefficient abomination O(n*n). Seems to work though: code:
code:
If min hasn't been assigned yet and the current value is greater than the current running minimum OR min is greater than the current value and the current value is greater than the current running minimum, set min to the current value. Essentially it looks for the smallest value that's greater than the current running minimum (which is the greatest minimum found so far). But is not efficient speed wise. Simpler to just sort the drat thing using one of the many available sorting algorithms.
|
# ¿ Nov 20, 2015 08:13 |
|
I have a question about a little piece of code that I'm writing: I have a base_controller class with a bunch of controllers that extend base_controller and override a bunch of methods in base. When a request comes in I dispatch the request to one of the appropriate controllers that I have registered for that particular request (I have a std::unordered_map<std::string,std::unique_ptr<base_controller>> that keeps track of which is which). The dispatch methods are all virtual overridden from the base_controller. Now, the dispatch methods ... are all the same, the difference only being the method that will get called in the corresponding controller. In order to write less code, I came up with this : C++ code:
But now I have method2 which has 3 parameters, but otherwise is the same as method1: api_controller::method2(const Request& request, Response& response, const std::vector< char >& body). Is there a way that I can modify call_sub_controller to work for both? I tried with variadic templates, but the call to the subcontroller ... went nowhere. That is, the program didn't crash, but it was like the call was not getting made, even though it reached ((controller->second.get())->*cb) line. Here's what I tried: C++ code:
Question 2: From the above code, all of the dispatch methods look like this: C++ code:
C++ code:
Edit: I am using Fedora 23 with gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2) with -std=c++14 Volguus fucked around with this message at 14:45 on Feb 9, 2016 |
# ¿ Feb 9, 2016 14:38 |
|
eth0.n posted:Possible solution .... Thank you. That's what I get for typing things from memory on a topic that I never used before, such as variadic templates. My code was compilable of course, just not working. Been reading in the last few days Meyer's Effective Modern C++ (11 and 14), and unfortunately I still cannot understand universal references and perfect forwarding. Either Scott is not explaining well or i'm just thick. Or both. It "looks" to be a relatively easy topic but, ugh ... I'll have to study harder I guess. Anyway, thanks for the pointers. Haven't thought about ODR violations, I'll fix and try again asap. Edit: This indeed solved my problem. Thank you. Volguus fucked around with this message at 00:57 on Feb 11, 2016 |
# ¿ Feb 10, 2016 20:19 |
|
I've recently heard about http://nanapro.org/en-us/ . It's modern C++ (not C) but from the examples it looks quite decent. It doesn't seem to be just a wrapper over GTK or other libraries, it looks like it does its own painting.
|
# ¿ Feb 29, 2016 01:37 |
|
roomforthetuna posted:Sure, but don't use new either if you don't know what you're doing. That's even worse because you're introducing memory leaks when you neglect to clean up, whereas with placement new at least you're only leaking sub-objects. What would you suggest to do when dealing with a C API that accepts a struct as a parameter, and that struct has a "void* user_data" member? And then you need that user_data later on, when you're far away from the original scope when the struct was created and initialized? Making an unique_ptr live that long would require quite a bit of refactoring plus not to mention leaking the API to the header and to whoever includes that. That can be solved with pimpl, but you are still left with the non-trivial refactoring problem.
|
# ¿ Mar 9, 2016 02:11 |
|
Ralith posted:Sometimes refactoring is necessary to allow resource ownership to be explicitly encoded. Do it, preempt the otherwise nearly inevitable leaks, and try to plan ahead better next time. I understand that and I agree ... usually ... but in this particular case the struct itself is the owner. What refactoring would do would destroy an otherwise very clean design with clearly defined boundaries. To leak that pointer outside the scope of the struct (in order to prolong its life) would essentially give ownership of that unique_ptr to a different entity who should not have any business in managing lifetime of those objects. Essentially, from what I can see, I can either maintain the clean design, with the downside of having to do a new and later on a delete (all encapsulated in a higher order unique_ptr with a custom deleter, since ... that's life in C) or break the design only so that I don;t have to do new/delete. Anyway, thanks for the input. Its been bugging me for a while that maybe I'm missing something, some better way.
|
# ¿ Mar 9, 2016 02:39 |
|
Ralith posted:Well, who owns the struct? Is there anything stopping you from exposing a wrapped version of it with a destructor, move constructors, etc? I could wrap it I guess, it would be a solution. I just wrote a paragraph explaining my dilemma, removed it, I guess I better just show some code (changed to protect the innocent and my company from killing me. it's from memory anyway): C++ code:
Volguus fucked around with this message at 04:32 on Mar 9, 2016 |
# ¿ Mar 9, 2016 04:30 |
|
Xarn posted:Because the whole thing is crazy ... No, that's not crazy, that's loving insane.
|
# ¿ Mar 27, 2016 06:13 |
|
|
# ¿ May 2, 2024 15:18 |
|
Joda posted:I want to start developing commercial software, which means my educational license for CLion isn't really gonna cut it anymore. Before spending €99 a year on a commercial license, I thought I'd ask if there are any good, free alternatives that work on Windows, Linux and OSX? Preferably with CMake as the primary system for project management, and it has to work with MinGW, gcc and its standard library >=4.8. QtCreator. Light years ahead of CLion. I personally prefer KDevelop on linux though (not sure if KDevelop works on other platforms).
|
# ¿ Jun 21, 2016 23:53 |