Hot answers tagged

72

A "raw" pointer is unmanaged. That is, the following line: SomeKindOfObject *someKindOfObject = new SomeKindOfObject(); ... will leak memory if an accompanying delete is not executed at the proper time. auto_ptr In order to minimize these cases, std::auto_ptr<> was introduced. Due to the limitations of C++ prior to the 2011 standard, however, it's ...


36

If you re-write & re-design from scratch, you're going to have two MASSIVE problems: You don't have a spec. You might think you have a spec, but it turns out that the REAL spec is the old code. So you're going to have to dig into it to figure out what it really does, just so you know what to make the new system do. You'll have a long period (looking at ...


15

I wouldn't refrain from using Qt just for those reasons. You are not required to use all of Qt's utility classes; for the ones that replace the STL, you'll at most be forced to use QString and, possibly, QStringList. Also, there's usually much more to a program than the GUI. You can always use exclusively generic C++ for the rest of your program, and use Qt ...


15

Copy on write is used in situations where you very often will create a copy of the object and not modify it. In those situations, it pays for itself. As you mentioned, you can pass a const object, and in many cases that is sufficient. However, const only guarantees that the caller can't mutate it (unless they const_cast, of course). It does not handle ...


14

The book you are reading was published in 2007. The C++ API for managing threads wasn't standardised until 2011. At the time, on different systems you had to use entirely different platform-specific libraries (pthreads, win32 threads, etc). Now, this is no longer true. Your book is out of date.


12

I could give you some rough guidelines as to how to create the equivalent GUI for a CLI app, design-wise. How you would actually make the calls is out of the scope of this answer. switches like -p -v etc are checkboxes mutually exclusive options are a group of radio buttons parameters that are a filename are a textbox with a "choose" button that shows a ...


12

Addressing the question about cleanup code: yes, there is a way, it's called RAII (for Resource Acquisition Is Initialization, an acronym whose limpid clarity is rivalled only by CRTP), and it is both idiomatic and highly recommended (eg, by the C++ Core Guidelines) You'll see there is built-in support for RAII for some common types in the form of std::...


9

Well, if you have the same kind of code multiple times, meaning switch on type-code and then use identical code with that type, how about using a template and generic lambda? template <class F> auto do_typed(int type, F f) { switch(type) { case CV_8U: return f(std::enable_if<true, std::uint8_t>()); case CV_8S: return f(std::...


8

What about creating a class to hold your arguments? This class would contain both open and close parameters and either of them could be NULL. Then, there will be only one strip method with above class as argument and method will decide if it wants to use open/close if they are set.


7

As you already discovered, putting all unit tests into one huge file is not a good practice (same as putting all classes into one file is bad as well, although it compiles a bit faster). You should create a subdirectory unittests where all unit tests go. It should be one file per class. This way you do not clutter your code with unit tests. If you can, ...


7

My quick two bits on this: 10 (sorry terrible joke). To expand slightly, here goes: 1- A static library can depend on another static library, nothing or even a dynamic library: in the first two instances all the code for the new static library would be incorporated in the new SLL (Static Link Library), however, the 3rd option, depending on evironment could ...


7

This shows that the author is using modern C++ (e.g. >= C++11) and applies good practice: Braced initialization is the most widely usable initialization syntax, it prevents narrowing conversions and it's immune to C++'s most vexing parse. -- Scott Meyers, in Effective Modern C++, Item 7 You have to be aware that there might however be a subtlety ...


7

I don't have any experience with QT, but I spent over a decade working with C++ and MFC. I now have almost a decade working with C# and WPF. I find that I am an order of magnitude more productive in C#/WPF than I ever was in C++/MFC. As a language, I find myself being far more productive in C# than I ever was in C++. Part of this is not having to worry ...


6

In modern C++ you typically want to use the Resource Acquisition Is Initialization (RAII) idiom. The idea is that any resource that you allocate should be wrapped into a class, and it should be deleted/closed/cleaned up by the class's destructor. A resource is anything that you need to de-allocate, close, or otherwise clean up when you are done with it. Most ...


6

When you declare something you just promise that during linking it will be available and uniquely identified. Static libs are nothing more than containers for definitions in a format which is outside the standard. So, this has the following implications: 1 ) I don't quite get what you're asking here, but one static library can very much depend on another ...


6

Usually, this can be solved by writing a GUI front-end that builds a command line. At that point you simply call the old CLI "main()" function with the arguments in the appropriate order. What you need to do then, depends on the output. You might do well by wrapping all printf()'s in a generic output variadic function, that in the CLI version will just pass ...


6

Rewriting a project of this size is doomed to fail due to the reasons in Michael Kohne's post. Refactoring a buggy codebase is difficult because you'll never know if bugs are due to your refactoring or due to the original code. I would take the approach of doing nothing but bug fixes for a significant period of time. As part of these bug fixes, you can ...


6

Saving the whole window as a single object into GPU (it would be bunch of rectangles saved as VBO) and then rendering it in a single OpenGL draw call would be fast, but it has several disadvantages: The whole geometry would have to be rendered using single shader. Having separate shaders (for opaque copy, transparent copy, gradient, ...) is more useful. The ...


6

Use of a software internally is not considered a conveyance by the GPL, so you wouldn't have to give source code to anyone. The moment you decide to distribute your software to any third party, then you'll have to follow the terms of the (L)GPL or pony up for a commercial QT license. As for static vs dynamic linking, you can static link with an (L)GPL ...


6

I have significant experience with C++ UI development, mostly in Qt but also including wxWidgets and raw win32. I've also done some C# UI development in WPF. I would strongly recommend literally any of the UI technologies I've used - including raw win32 - over C#/WPF. In my experience it's poorly documented, incredibly slow, and all of its abstractions leak ...


5

When I've implemented large scale re-factoring on projects, I've generally used the strategy of doing the architecture afresh, and proving the architecture first, whilst development continues on the original project. When the new architecture has been proven, there is a large amount of transferring existing logic to the new architecture. This seems to be ...


5

An implicit cast to first base type and the reverse static cast to derived type when you are certain it is that type are exactly zero operations at runtime. You can't beat that. An implicit cast to non-first base type and the reverse static cast to derived type which has the base as non-first is a simple increment/decrement with a null pointer check. It has ...


5

If you know that everything in the scene is an instance of Sprite, then there should be no problem with typecasting. You can enable RTTI and use dynamic_cast<Sprite *> to be extra certain. If there may be other GraphicsItem elements that are not instances of Sprite, then you could keep a set (perhaps QSet<GraphicsItem *>) which holds pointers to ...


5

on my own opinion, learning C++ programming is simpler than falling into other languages that hide their complexity, and programmer does not know what really happens in background. Qt on the other hand, adds some benefits over C++, to make it higher level than native C++. Thus Qt C++ is a great framework for who wants to develop low-level tasks, or high-...


5

I agree with most of high praise of Qt, but the question was What's the best GUI framework to use that allows/requires the most use of generic C++ and the STL? In this respect Qt is a little schizophrenic: it duplicates STL containers and algorithms with it's own twists. It also provides containers, which are different than STL. Interoperability between Qt ...


5

There are many options, it's your tradeoff which to take: Decision at runtime: Add a defaulted bool argument: QString MyClass::strip(QRegularExpression regex, bool close=false); // Mimic the two-regex-variants interface as good as possible Use scoped enum's and no default as a variant on 1 which is more descriptive: enum class option { open, close }; ...


5

Under the hood, it might be implemented differently, but the visible effect of MyClass(const QByteArray & raw){ this->m_rawData =raw; } will be that the contents of raw get copied into m_rawData and will survive after raw has been destructed. This works because m_rawData is declared as being a value of type QByteArray. If it would have been ...


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