Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [c++]

Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.

57
votes
9answers
8k views

Readability versus maintainability, special case of writing nested function calls

My coding style for nested function calls is the following: var result_h1 = H1(b1); var result_h2 = H2(b2); var result_g1 = G1(result_h1, result_h2); var result_g2 = G2(c1); var a = F(result_g1, ...
1
vote
3answers
690 views

Architecture of gui application

I am writing gui application. I want to implement following structure: project tree with nodes of different type and behavior (i.e. when right clicking or selecting there can be different menu ...
1
vote
1answer
624 views

How to control optional dependencies of a header-only library in `CMake`?

I am developing my own header-only library that I want to use as a framework in other software. I am using CMake for setting up demo targets, tests, and so on. However, I am unsure how to deal with ...
1
vote
2answers
629 views

what is an empty method and how are they used?

what is an empty method and how are they used? I was reading a documented about the BUILDER Pattern and I got a curious about how it is implemented in C++. The author defines that in C++ could be ...
0
votes
3answers
623 views

Memory segmentation - stack, heap, etc.?

So memory segmentation can be done with or without paging. I always hear people talking about stack and heaps when discussing something memory related in C++. However, what I do not get is that if the ...
0
votes
0answers
90 views

How to translate from managed to native class definitions

I have an input file, being the DLL of a C# piece of code, containing managed class definitions. I also have a *.json file, who determines how the conversion should be done. As an example, this is ...
1
vote
3answers
260 views

design pattern for class with data attached

I have a class Tiles that looks something like this: class Tiles { public: void AddTile(int x) { tiles_.push_back(x); } std::vector<int> tiles_; } Now I want to create a class Tiles with ...
3
votes
3answers
138 views

How to implement e-book reader page scrolling with variable page size?

I'm a CS student and in order to practice my coding skills I'm trying to implement an e-book reader and I want some advice from more experienced programmers. I'm using C++\QML but I'll try to keep my ...
0
votes
1answer
171 views

Beginner C++ question about program design with unique_ptr and vectors [closed]

I am currently making a console program about buying property in a city. Properties can be bought from the city by someone, transferred to another person, or returned to the city if the person who has ...
2
votes
3answers
208 views

Are Instanced APIs a Problem in a C Interface?

So an Instanced API is one that behaves like an object. So for example: foo* GetInstancedAPI(); void MemFuncSetter(foo* fooThis, const int arg); int MemFuncGetter(const foo* fooThis) const; This is ...
2
votes
3answers
805 views

How is dependency injection different from simple interface usage?

While trying to solve an issue, explained on the StackOverflow forum, somebody advised me to use dependency injection. For personal reasons, the moment a person mentions to me the usage of a design ...
2
votes
1answer
94 views

Reusing UI elements between two different QWidgets

I am writing a utility in C++ with Qt which communicates with an embedded device. The program was originally going to be used to just plot data from the device, but a new requirement has been added (...
1
vote
4answers
387 views

Unit test a generic floating point equality function

I've written a function that tests two floating point numbers for approximate equality (see this Code Review question). I'd like to unit test my function, but I'm not positive of the best way to do ...
7
votes
1answer
429 views

Handle Optional Logging in High-Performance Library

I'm writing a library in C++ which needs to be as fast as reasonably possible. However, I'd also like to be able to provide logging in case a user (or me) needs to debug possible problems. This ...
11
votes
5answers
810 views

How to design exceptions

I'm struggling with a very simple question: I'm now working on a server application, and I need to invent a hierarchy for the exceptions (some exceptions already exist, but a general framework is ...
-7
votes
1answer
319 views

Importance of storage in c++ [closed]

I want to know that whether storage is an issue with respect to current hardware and software market when we talk about the large size of C++ programs?? Because in certain conditions we have to choose ...
21
votes
4answers
3k views

Use abstract class in C# as definition

As a C++ developer I'm quite used to C++ header files, and find it beneficial to have some kind of forced "documentation" inside the code. I usually have a bad time when I have to read some C# code ...
7
votes
3answers
1k views

How to use 'Adapter' without any changes in the existing code in c++

Here we have TV class and DVD class as example: class TV { public: TV(); virtual ~TV(); void on() const; void off() const; }; class DVDPlayer { public: DVDPlayer(); ~...
3
votes
2answers
835 views

“Crossing Boundaries” in Clean Architecture

I am a layman without any programming-like education but I spent the better part of my free time to get into programming bots for some games in first AutoIt and then C++. I was introduced to ...
1
vote
0answers
50 views

How should I set up the listener service for a dynamic user interface?

So I'm working on a project and I'm running into conceptual problems in creating my user interface. It's for a DirectX11 multi-monitor game I'm writing. I've got a prototype working, with entity ...
1
vote
1answer
573 views

When writing a Qt application is good practice to ALWAYS prefer Qt function implementations, data types, and classes when they are available?

I'm just starting out with Qt and I really want to try and keep my application as separated from Qt as possible in case I decide to use a different toolset later, but at the same time don't want to ...
4
votes
2answers
176 views

Is there a valid use case for the using namespace directive?

I looked trough some of my older code and found that I was using the using namespace directive. From what I read in a lot of google results, it seems that it is never a good idea to use this. Is there ...
2
votes
3answers
298 views

Should I divide C++ program into multiple interacting applications?

We are discussing proposal to split a big C++ program into multiple separate executables that would communicated using shared memory. The shared data structures are large, so we do not want to use ...
14
votes
8answers
4k views

Should there be assertions in release builds

The default behavior of assert in C++ is to do nothing in release builds. I presume this is done for performance reasons and maybe to prevent users from seeing nasty error messages. However, I'd ...
15
votes
3answers
3k views

When should I use string_view in an interface?

I'm using an internal library that was designed to mimic a proposed C++ library, and sometime in the past few years I see its interface changed from using std::string to string_view. So I dutifully ...
1
vote
1answer
59 views

architecture of gui app with several indepedent windows and sqlite backend

Let's assume that I have such a simple database scheme: CREATE TABLE tbl(key INTEGER PRIMARY KEY, cap TEXT NOT NULL); I want to show at least three independent GUI windows. One window (window A) ...
2
votes
0answers
90 views

Access modifiers in combination of interpreter pattern with template method pattern

Motivation: (Skip to "The Problem" if you don't need motivation for it) As a project for myself, I'm writing an expression parser for certain kinds of mathematical expressions, and I'm using the ...
1
vote
1answer
316 views

How to organize the management of resources for OpenGL?

I write my own 3D-graphics engine for education and have some difficulties with it's architecture. I wrote classes like OpenGLTexure, OpenGLMaterial, OpenGLGpuProgram, etc. I also wrote a class ...
9
votes
2answers
4k views

Proper design to avoid the use of dynamic_cast?

After doing some researches I can not seem to find a simple example resolving a problem I encounter often. Let's say I want to create a little application where I can create Squares, Circles, and ...
3
votes
1answer
165 views

How do you add feature to a class that was originally designed wrong in the first place? [duplicate]

I have a Surgeon class that is constantly changing class Surgeon { string name, discipline; public: Surgeon(string _name, string _discp) : name{_name}, discipline{_discp}{} void writeDir(...
58
votes
3answers
8k views

What's the reason for not using C++17's [[nodiscard]] almost everywhere in new code?

C++17 introduces the [[nodiscard]] attribute, which allows programmers to mark functions in a way that the compiler produces a warning if the returned object is discarded by a caller; the same ...
1
vote
1answer
113 views

Inheritance or strategy in my circular buffer

I've looked at some other threads but they don't quite answer my question, I think... I have a CircularBuffer object which I have separated from a ThreadSafeCircularBuffer object so that the CB logic ...
6
votes
6answers
387 views

Exposing member variables vs Adding member functions to modify them

Say that we have a class Car that contains objects of type Wheel class Wheel { public: void SetFriction(double f) { friction = f; } private: double friction; }; class Car { ...
0
votes
1answer
212 views

Adding error handling to a function (which sometimes should throw an error and sometimes use a default value)

I'm trying to add error handling to a function that reads data from a DataSource (which performs all the parsing etc. required). Unfortunately (due to a lack of versioning until now) some of the ...
27
votes
4answers
8k views

Using #ifdef to switch between different types of behavior during development

Is it a good practice to use #ifdef during development to switch between different types of behavior? For example, I want to change the behavior of existing code, I have several ideas how to change ...
2
votes
1answer
195 views

Multicast function container

I'd like to implement a multicast function. Currently, I'm in the design phase of this. The basic idea is to have a class, to which std::function can be added, an interface like this: template <...
5
votes
2answers
326 views

Should We Use Protected Member in Base Class Or Private Member in Derived Class?

The reasons may vary from breaking principle/guidelines of OO to considering protected as part of public API. Then, what should we do if some derived classes have same members? Should we declare them ...
3
votes
1answer
355 views

C++ Are members of a class pointer automatically on heap?

Let's say we have a struct Vector2i { int x = 0, int y = 0 }; And create a Pointer to it via Vector2i* pointer = new Vector2i; Where would int x and int y be stored? Heap or stack? Are all members ...
42
votes
3answers
7k views

What's the difference between a subclass and a subtype?

The highest rated answer to this question about the Liskov Substitution Principle takes pains to distinguish between the terms subtype and subclass. It also makes the point that some languages ...
4
votes
2answers
513 views

Can I use C-style casts when calling C functions from C++?

I have read, both on this site and elsewhere, that the recommended programming style for C++ is to avoid using C-style casts, and prefer the C++-style static_cast, dynamic_cast, reinterpret_cast, and ...
3
votes
4answers
98 views

Store csv data as rows or columns in view of the needed processing?

Assume I have some data a csv-Files like ObjectName, PropertyA, PropertyB, PropertyC "Name1", 3, 1, 4 "Name2", 1, 5, 9 "Name3", 2, 6, 5 ... and a typical question I want to answer would be For ...
5
votes
1answer
1k views

Should I cast comparisons to NULL or nullptr?

Let's say I have a C++ function that looks like this: bool Foo(Bar* b) { if(b == nullptr) { return false; } // Do stuff return true; } Let's further say that, because of ...
2
votes
1answer
162 views

Software architecture of IoT hardware controller program with embedded HTTP server

Just trying to design an architecture/class relations for my current C++ project in the IoT field. The program itself (controller) controls various hardware via an arm-based device. I intend to use ...
2
votes
0answers
466 views

How to organize project code with googletest?

Generally, we like to have a common folder for common header files to be shared among various projects in solution but what about googletest since it needs access to all header files of project to ...
1
vote
1answer
164 views

Design pattern for modeling a PC

I have a type of "PC" (i.e. personal computer) that I need to model in C++ (can use language standards as new as C++11, but no newer than that). In short, we have a lot of granularity for components ...
4
votes
1answer
257 views

Are externs (global variables) ever justifiable?

Under which circumstances would you deem it justifiable to use externs (i.e. global variables)? For example, in a system with 1 given state at a time, would it be appropriate to store this state in ...
5
votes
4answers
989 views

If you follow the rule of Zero, how will you debug the construction of an object?

Everyone is quick to point out the "Rule of Zero" in code reviews, peer conversations, and Stack Overflow comments/answers. I am not a believer. I'd like to be. Usually if enough people agree on ...
-2
votes
1answer
205 views

Improve design with singleton pattern

I want to create a media management tool, for which I try to draw an appropriate UML class diagram. The tool shall hold a collection of different media types (movies, music, etc.) which inherit from a ...
1
vote
2answers
223 views

Good practice to store runtime data

Cheers guys, I am quite new to C++, however, I have some years of experience in programming and I am open for challenges :-) Currently, I am designing a "small" scientific program to perform some ...
1
vote
2answers
650 views

How to return boolean result from comparison loops to maintain better readability?

Let's consider I have an std::string instance filled with textual data and an std::set<std::string> instance with keywords. I would like to know whether the text stored inside the std::string ...