Tag Info

Hot answers tagged

18

allow iteration without leaking the internals is exactly what the iterator pattern promises. Of course that is mainly theory so here is a practical example: class AddressBook { using peoples_t = std::vector<People>; public: using iterator = peoples_t::iterator; using const_iterator = peoples_t::const_iterator; AddressBook(); iterator ...


13

This is actually a well-known concept called bootstrapping. Basically, there exists, somewhere, a minimal C codebase to build a version of GCC that's capable of building the current GCC codebase. Self-hosting languages have been doing things like that for decades.


13

The pure fact you don't understand this function should be a big warning sign not to implement your Tic-Tac-Toe winning test in a similar manner. If you do, you will probably not understand your own code in a few weeks any more. Instead, use a two-dimensional array or vector, and some loops to check the winning conditions. And don't think too much about ...


9

Your problem isn´t having a class with a constructor method, your problem is what you have put into it. Parsing a text file should not be part of the object - its job is to create and manage Simulations. The constructor/init method should take a simple list or map of particles as one of its parameters and use that data when initialising. Reading data from ...


9

Programs which require real-time number crunching (such as digital audio workstations or video players) have what I call a "computational threshold." What that means is that the choice of programming language can matter when there is not enough hardware horsepower to satisfy the necessary computational load, if the language itself is consuming a substantial ...


7

However, though versions of this text appear all over on the internet my question is not completely answered by this explanation. Though it may be implied, since global variables always seem to be static it would be nice to have a straight out answer. If you didn't explicitly create it, you don't need to explicitly destroy it. Let's just remove some of ...


5

There is absolutely no purpose or benefit in such. One of the goals of C++ was to treat UDTs and primitives interchangably, and whilst they haven't entirely succeeded, this is one area where you don't have to differentiate. When it comes to naming, Stroustrup is nuts, and this is a scientifically-proven fact.


5

Creating a compiler that is written in the same language that it compiles is called bootstrapping. The wikipedia article describes a number of ways that a compiler can be bootstrapped. Given your restriction that you only have a post-4.8 G++ source code and no pre-built binaries for your target platform (no existing C++ compiler), then bootstrapping the G++ ...


4

Wrap all access to the variables in a thread safe function so that access of the variables from anywhere in the code flows through a single method, and make the variable private. This way any access of the variable from outside the class will be thread safe. The next step is to move the variables into their own class, only accessible via the safe methods. ...


4

It depends. If you need to calculate once, and use million times, then the answer is obvious: the first case wins. If you need to calculate every time, or quite often, then pick what you prefer. Your function doesn't read from a file, and has no slow operations, so it will not make a huge difference. Avoid premature optimization. When optimizing, it is ...


4

Python is a dynamically-typed language. This has two important consequences: The compiler is unable to reject certain kinds of logical error at compile time which would be caught by a C++ compiler Because the types of some (even most) variables cannot be determined at compile time, operations on those variables must be implemented by dynamically ...


3

A field you add to a structure for the purpose of error checking is often called a dog tag (I think it's so called after Code Complete - Steve McConnel). A dog tag is used to check for corrupted memory: when you allocate a variable put a value that should remain unchanged into its tag file; when you use the structure check the tag's field value (if the ...


3

For discussion, I'm going to present a counter-argument to @itsbruce. YMMV. What is more likely to change, the file format or the object itself? (as in additional or changed fields). In my experience, if the file format is standard, and accessed through abstract interfaces (e.g. standard XML, JSON libraries), the core code for that doesn't change. It's ...


3

What you need to understand is that the C++ standard library quite possibly has the worst implementation of futures conceivable by man. What C++ does is little more than a weak wrapper around a semaphore. Most future implementations encourage use of callback lambdas extensively, to chain several futures together asynchronously using then, and allow you to ...


3

This is a simple C++ rule: objects that are more complex that a simple integer, pointer, etc (not a native type) have constructors and destructors. When an instance of such object is declared by value inside a scope, its constructor gets automatically called. When the scope it was declared is exited, the destructor gets automatically called. So take this ...


3

Generally, what is the preferred method for the problem described above, or which method is preferred in which cases? The prefered method looks like this: class Object { ... }; std::istream& operator>>(std::istream& in, Object& obj) { /* classic "in" operator*/ } Client code (1): Object o; if(std::cin >> o) { // do ...


3

Don't confuse "exception" with "rare" The main point of exceptions and exception handling is keeping different types of code apart: The code that provides the main functionality (the "happy path" code) Everything else: The code that handles all those 973 other cases where not everything has worked out wonderfully and precisely to your main ...


3

Think of it as a binary number where all the ones are your pieces. To simplify my explanation, I'll just consider one row of 8. The rest all work similarly. Consider a winning case of four in a row: 11110000. board && (board >> 1) gives you 1110000, which is all the ones with a one immediately adjacent to the left. Doing this again with y ...


3

In older C++ (pre C++11), there is no significant difference between your two implementations of operator+. Either the copy constructor gets called when you invoke the operator or when you make the explicit copy inside the operator. In this case, it is more of a personal preference/coding guideline issue which one to choose. With the introduction of move ...


3

There is no clean and safe way to quickly/efficiently "kill" a thread. You can signal to the thread object that it needs to terminate, but the thread needs to be written in such a way that it checks for this and then cleans itself up. Otherwise, you can end up with partially-completed operations, memory leaks, resource leaks, deadlocks, and all manner of ...


2

Using bits to represent a Tic-Tac-Toe board is perfectly fine and is as easy and maintainable as using two dimensional array. Since there are only 8 possibilities for winning condition, you can just check the board against every cases. 1 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 0 ...


2

I know nothing about smalltalk, so I'll only talk C++. In C++ (and AFAIK in Java and C#), each thread has its own stack. If you don't know what a thread is, I suggest you read about it. I'll describe it here as the bus your code is taking a ride on. But I suggest you learn more than this horrible analogy (Also, not all C runtimes are multithreaded, so there ...


2

There are two elements to this problem: #1, the technical change to the existing code base to support multi-threading, and #2 changing the culture of the company in order to be aware of thread safety in the code. The first is relatively simple. First, document the functions in the class that are thread safe and are not thread safe. I agree with Jeffery ...


2

Adding multi-threading is a major new feature of a class. I think you should treat it like a major new feature, and don't try to sneak it in. Rethink and refactor the class and it relationships. I would look into creating a new class to represent each threaded task and/or slice of functionality. If needed you can preserve the old class interface as a proxy ...


2

On the newMNumber create a unique_ptr and use the .get() method to return a raw pointer. Wouldn't this defeat the purpose of using smart pointers? No, no it would not. The point of using smart pointers is about controlling who deletes the object, not who observes the object. It's perfectly fine under the smart pointer paradigm to observe the ...


2

Ok, I would never in a million years do this, but you could do something like this: Force auto inclusion of this: // forcebanned.h #define for "You must include banned.h" Then add this: // mybanned.h #undef for #include banned.h But don't do that. Better is to make the inclusion of banned.h a coding standard, and rely on code reviews to catch it. If ...


2

If iteration is all you need, then perhaps a wrapper around std::for_each would suffice: class AddressBook { public: AddressBook(); template <class F> void for_each(F f) const { std::for_each(begin(people), end(people), f); } private: std::vector<People> people; };


2

This is probably a bit opinionated, but if I see two equivalent alternatives in code, I typically prefer the one with less "noise". In your case, the difference of the second alternative to the first is only in additional "noise" - technical code which does not improve the readability in any way. That is why I would prefer the first variant. or should I ...


2

I've never used OpenCV, but AFAIK, it is very different from OpenGL. OpenGL is a C API constructed around a LOT of global state (hence why it is often called a state machine), while OpenCV exposes a more Object Oriented C++ interface, with heavy use of templates. OpenGL is intended to be a thin abstraction layer of the graphics hardware, so it can also be ...


2

OpenGL is a 3D graphics API. It provides APIs describe a 3D scene and render it to a framebuffer and ultimately display it on a screen. The primitives it has are vertex lists, triangle lists, normal vector lists, etc. n.b. 2D is a special case of 3D; IIRC OpenGL doesn't have explicit 2D support (i.e. sprites and bit blit) OpenCV is a computer vision (CV) ...



Only top voted, non community-wiki answers of a minimum length are eligible