Tag Info

New answers tagged

0

The bottom line is my brain has been wired to think about Objects, Encapsulation, Polymorphism, etc.. and I cant seem to wrap my head around anything else. How can I practice? The biggest – if not the only – difference between procedural programming and object-oriented programming is the notion of inheritance.¹ For instance, it is straightforward to ...


3

A lot of procedural code is very OOP-like. Basically, instead of object.function(params), you do function(object, params). You can group your files accordingly. However, what a lot of long-time OOP programmers don't realize is how limiting it is that a function must belong to one and only one class, and that all such functions must be grouped into one ...


0

I have way more familiarity with procedural programming than OOP. I basically use only ANSI C and recently Lua (for WoW addons). Not sure if appropriate or not but the very basic thing that brings you to OOP from procedural programming is when you start to dislike isolated variables that are conceptually tied together. Today for example I wrote a program ...


3

The way we used to do sort-of OOP back in the days of C was by declaring a struct and then declaring functions that accept such a struct as their first parameter. Then sometimes the need for polymorphism would kick in, so we would have our struct contain not only data fields, but also pointers to functions. It is quite pointless, really, doing by hand what ...


1

The way I see it, to go from proper object-oriented programming to a procedural style, you need to: remove behavior from your objects; make them just containers of data (data structures) implement behavior in separate classes (services); they will take the data structures as arguments and perform some action according to application use cases use the same ...


4

To be honest, this seems a bit like going backward and programming with one hand behind your back, but if by "structured", you mean, like how people created programs before Object Orientation in procedural languages, then it's about how you start thinking about the problem. In your fourth paragraph, you are essentially still thinking in an Object Oriented ...


2

I would think that the best thing to do would be to employ a tool like Xalan or Xtrans or any other XSLT processor to implement an intermediate XML transformation step, so that your software needs to know only one naming convention. The ability to perform such transformations is one of the major reasons why XML was invented in the first place.


1

This depends on the game and its specific implementation. If the game provides a programming interface for "bots", use that. If you are the author of the game or have the source code available, provide such an interface. You may consider to learn some bits about interprocess communication, specficially on Linux, first. If you are thinking about controlling ...


-1

Like others, I will question the need for C++. If you consider JAVA programming, instead of C++ (Meanwhile, I enjoy programming in ANSI-C, but I think your priority is a rapid development, instead of the joy of programming). Please check the site http://www.json.org/java/ Actually http://www.json.org/java/ might give you also some ideas for C++ libraries. ...


3

IT depends on the frequency of the task in your hand. If it is going to be one time job, or let's say once in a year job, do not worry about the speed, reduce the complexity of your code (independent from the language you are planning to use). Otherwise, if such XML files are created every day / hour and require JSON conversion, I will even prefer C since ...


0

There is no standard graphic library in C++. You will have to use a non-standard library. The choice will depend on what you want to achieve. If diplaying text in the console is enough, you can use the standard streams (see ) or printf() (which is a C function). If you really want "graphics" like in displaying images/sprites and moving them, a lot of ...


-1

You have to export when compiling the dll, but import in the code using that dll interface. It is very common in C++ to use macros to do this automatically, that is detecting if we are compiling the dll's code or not and exporting or importing symbols respectively. A lot of libraries providing dll/so implement this kind of macro, so you can find a lot of ...


3

As long as the underlying hp value is not public, it doesn't matter too much either way. Having Entity::hurt()/heal() directly access a private hp member is fine, and making them wrappers for a Health::hurt()/heal() call is also fine. Assuming it's not public, I would base the decision on what you expect to happen in the future. Will health probably be a ...


6

From the comments... if everything else you have is C++ then the best answer is to write it in C++, building a mish-mash of different bits of programming languages is a right PitA to maintain and support. So, if you have C++ and need to resolve XML to JSON, it seems obvious to use the xml2json library that you linked to. It comes with sources so you can ...


3

Let me provide a different, yet typical, answer: it depends. A lot of people (me included) have troubles with inheritance hierarchies such as the ones @greyfade mentioned in the other answer. The clean code mantra "prefer composition over inheritance" stems from exactly that. However, by understanding the problems that are at the root of inheritance ...


3

Is there anything wrong with this approach? Is it at all typical to have "top level" classes that are more or less made up of several other base classes? Yes, and yes. Historically, it has been common practice among game engine developers to build their game entities in exactly the way you describe. I have, in fact, several well-regarded books that ...


0

This answer complements @lxrec answer. Makefiles can be used for many things, not just creating a program/library from source code. Build systems such as CMake or autotools are designed to take code, and build it in such a way as to fit into the user's platform (i.e. find libraries or specify correct compile options). You could for example have a makefile ...


0

I agree with your reviewers and with @utnapistim. You can use system_error approach when you implement cross-platform things when some errors require special handling. But even in this case, it is not a good solution, but less evil solution. One more thing. When create exception hierarchy, do not make it very deep. Create only those exception classes, that ...


3

But I cannot get my head around to how the boost library does this. The boost interprocess mechanism has three necessary components to work: memory-mapped file: a memory-mapped file needs to be created and passed to a boost.interprocess allocator. This allocator will take chunks of the file and use them as if they were returned by a std::allocator, ...


-1

What makes you think the index value or the required range or the name of the table is a useful detail, for an exception? Exceptions aren't an error handling mechanism; they are a recovery mechanism. The point of exceptions is to bubble up to the level of code that can handle the exception. Wherever that level is, either you have the information needed, ...


2

Boost uses memory mapping of a file. Both unix and windows support creation of files that don't exist on the normal file system for just this purpose. Then you will need to synchronize access to that memory like you would if different threads were to access it. Meaning concurrent reads can happen without synchronization but as soon as one process want to ...


2

Shared memory is still just memory. You can put a mutex, spinlock or any other synchronization primitive in there, and use them to synchronize your processes' access to the shared memory, exactly like threads use those primitives to synchronize access to the memory visible to them. The only real differences are: threads share all memory and the same ...


3

shared memory is not the complete picture for IPC, its a data-passing mechanism but you still need some way to inform the other process that some data has been updated and is available to be read. How you do this is up to you, typically you'd use an OS mutex or event object, each process waits on this to be set, the application writing sets it once its ...


2

Notice that C and C++ are different languages. Shared memory is impossible in purely standard C11, or C++11 (since the standard does not define that), or even C++14 (whose n3690 draft, and presumably official standard, does not mention shared memory outside of multi-threading). So you need extra libraries to get shared memory. But some operating systems ...


8

I think your colleague was right: you are designing your exception cases based on how simple it is to implement within the hierarchy, not based on the exception-handling needs of the client code. With one exception type and an enumeration for the error condition (your solution), if the client code needs to handle single error cases (for example, ...


1

I don't think that human written Makefile-s are obsolete, especially when: using POSIX make, which gives you a portable Makefile or using GNU make 4, which gives you many very interesting features, in particular GUILE scriptability, which enables to code efficiently the fancy features provided by Makefile generators (I believe that the features of ...


1

Makefiles are not obsolete, in the same way that text files are not obsolete. Storing all data in plain text is not always the right way of doing things, but if all you want is a Todo List then a plain text file is fine. For something more complicated you might want a more complicated format like Markdown or XML or a custom binary format or anything in ...


0

To give a slightly different answer: The offending code has probably been done to spec: The function receives X and returns Y If X is invalid, throw exception Z Add the pressure to deliver exactly to spec (for fear of being rejected in review/testing) in minimum time and with minimum fuss, then you have your recipe for an entirely compliant and unhelpful ...


0

While I do agree that exceptions should contain as much information as possible, or at least be less generic. In the table not found case, the table name would be nice. But you know more about what you were trying to do at the place in the code where you received the exception. While you often really can't do much to rectify the situation when something ...


0

Exceptions have a language- and implementation- specific cost. For example, C++ exceptions are required to destroy all the living data between throwing call frame and catching call frame, and that is expensive. Hence, programmers do no wish to use exceptions a lot. In Ocaml, exception throwing is nearly as fast as a C setjmp (its cost does not depend upon ...


1

First off, let me burst a bubble by saying even if the diag message is loaded with information that brings you to the exact code line and sub command in 4 seconds, chances are the users will never write it down or convey it to the support folks and you will be told "Well it said something about a violation... I don't know it looked complicated!" I've been ...


2

make (the tool or direct use of it via a Makefile) is not outdated, particularly for "small, personal projects" as you use it for. Of course, you can also use it for larger projects, including those targeted for multiple platforms. With target-specific variables you can easily customize how you build for different platforms. Nowadays, Linux distributions ...


7

I don't have an excess of C# experience, or C++ specifically, but I can tell you this - developer-written exceptions 9 out of 10 times are more useful than any generic exception you will ever find, period. Ideally yes, a generic exception will point you to exactly why the error occurred and you'll be able to fix it with ease - but realistically, in large ...


143

Exceptions do not contain useful details because the concept of exceptions has not matured yet enough within the software engineering discipline, so many programmers do not understand them fully, and therefore they do not treat them properly. Yes, IndexOutOfRangeException should contain the precise index that was out of range, as well as the range that was ...


33

Why is it that many common exceptions from system components do not contain useful details? In my experience, there are a number of reasons that exceptions do not contain useful information. I expect that these sorts of reasons would also apply to system components - but I don't know for sure. Security focused people see exceptions as a source of ...


4

The question is specifically asking why do so many exceptions thrown by "system components" (aka standard library classes) not contain useful details. Unfortunately, most developers do not write the core components in standard libraries, nor are detailed design documents or other design rationale necessarily made public. In other words, we may never know ...


6

Is make really outdated? I don't think so. In the end, make is still powerful enough to provide all the functionality desired, like conditional compilation of changed source and alike. Saying make was outdated, would be the same as saying writing custom linker scripts was outdated. But what raw make doesn't provide, are extended functionalities and stock ...


3

Once upon a time high level languages were just an idea. People tried to implement compilers. Back then there were severe hardware limitations - there were no graphical tools so "plain text" ended up being used for the input file format; computers typically had an extremely tiny amount of RAM so the source code had to be broken into pieces and the compiler ...


-2

if your project is simple and contains very few files, then no make file is needed. However, when the project is complex, uses numerous memory areas, has many files, then it is necessary to place each memory area in the right spot in the addressable memory, it is highly desirable to not recompile every file every time a trivial change is made to one file, ...


15

The big difference is that CMake is a cross-platform meta-build system. A single CMake project can produce the usual Unix/Linux makefile, a Visual Studio project for Windows, an XCode project for Mac, and almost any other non-meta build system you might want to use or support. I wouldn't say using make directly or even manually editing makefiles is ...


1

but adding that dependency to all those classes doesn't sit well with me. If each array is encapsulated inside a data class, it makes sense to add an overloaded operator to that class to lookup the elements. // This code only illustrate the overloaded operator. // The code here is incomplete, and by itself is unfit // for all purposes. // // ...


0

Aside from using a free function, you may want to examine your class decomposition. If you have a lot of classes which have a 3D matrix flattened into a 1D, and they all access that matrix via this calculation, it's likely that they should all have a common class threeDinOneD which they use.


0

Historically, Undefined Behavior had two primary purposes: To avoid requiring compiler authors to generate code to handle conditions which were never supposed to occur. To allow for the possibility that in the absence of code to explicitly handle such conditions, implementations may have various kinds of "natural" behaviors which would, in some cases, be ...


11

Given that your question is tagged c++, do not use macros! They should only be used for include guards and a few other esoteric uses. The proper solution here is a standalone inline function. Assuming that WIDTH and DEPTH are constants that are visible to the function, here is its definition: inline int f(int x, int y, int z) { return x + WIDTH * (y + ...


0

One way to handle these is to have factory functions: expected<Missile> Missile::Create(Params...) expected<unique_ptr<Missile>> Missile::Create(Params...) class Missile { private: Missile() = default; int Initialize(int power) // second phase constructor { // other operations (besides out_of_memory) that may fail // can ...


6

C++ can do it the same way C does. All C++ gives you is easier-to-use containers that wrap much of the low-level detail. For example, a string class can (and does) hold a block of memory on the stack for short strings, only allocating a heap buffer for larger ones. This buffer is exactly like a C string buffer, if the string resizes, the string class will ...


3

Default c++ constructors should be avoided if that means leaving object in a partially constructed place. This is obvious a good advice but things get complicated when you also disallow exceptions (as it does) Not necessarily. Consider these rules: a constructor should receive already validated arguments, and perform no operations outside of ...


0

There are three errors: Your headers declare a name in a namespace not private to your library which they have no business declaring. You seem to not make sure there's only one definition of it in your whole library. You are trying to shut the warning up instead of correcting that bug. What you should do is one of: Make it a part of your interface. Or ...


2

I typically use one of the two ways. First way is to typedef at the place-of-first-declaration. Second way is to typedef at each place-of-use, and make it only visible to that place-of-use (by putting it inside the class or method that uses it). (1) Put the typedef close to the type that is being wrapped. /* MyAttrType.h */ #include ...


7

Mandatory disclaimers (1) Because people who have seen the code can't say anything about it, and people who can freely comment on it have never seen the actual code, all we can do here is to speculate, speculate, and to speculate. Therefore, here is not an answer, just a speculation. (2) This is not the typical way I write C++ because most of the projects ...



Top 50 recent answers are included