Questions tagged [c++]
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
2,715
questions
0
votes
4
answers
80
views
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
In C# and C++, an apparent trend is in place to reduce / avoid inheritance:
C#: "Sealing types can improve performance."
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/...
1
vote
1
answer
64
views
Should private static constants be in declaration (header) or defintion (source)?
Given a class which has certain private, constants (e.g., configuration), should these (A) be included in the declaration of the class (in the private section) or (B) should it be "hidden" ...
0
votes
0
answers
38
views
Should I separate algorithms that rely on a data structure into different classes
I'm learning about graph algorithms, so to learn them better I'm beginning to write some of them using c++. For example, I created the code for implementing the Depth First Search algorithm. Here's ...
1
vote
1
answer
442
views
Should I use private field or static variable in my method?
There is a variable that must persist between calls but otherwise only one method uses it.
As it is a method of the object, the value can be persisted as a private field of the object, or otherwise it ...
0
votes
0
answers
59
views
Why std::visit with multiple variants create n-dimensional array instead of nested visit calls?
variant<int, char, double> vr1 = 56, vr2 = 0.4674, vr3 = '4';
visit([&](auto v1) {
visit([&](auto v2) {
visit([&](auto v3) {
// access vr1, vr2, vr3 values
...
0
votes
3
answers
125
views
Is there a distinct optional type semantically representing a value that *needs to be calculated later*?
Semantically, C++ std::optional, Rust Option<T>, and other optional/nullable types represent a value that can be present or absent: you have to handle both cases, or you can opt-in to crash.
Is ...
0
votes
1
answer
69
views
C++ creating a class with self referencing custom comparator [closed]
This is the node for a tree
class Node {
public:
string name;
map<string, string> attribute;
Node* parent;
set<Node*, customComparison> children;
};
Where customComparison in ...
0
votes
1
answer
88
views
Options for designing a generic library
During development of libraries (mainly for use in internal projects) I have come across the "problem" of how to design them in a generic way. I am going to demonstrate with an example ...
0
votes
1
answer
81
views
Comparing synchronization primitives (future, barrier, conditional var) - which one fits the best?
Trying to understand and compare synchronization primitives, like std::future/std::promise, std::barrier and std::conditional_variable - developing an intuition for which one to use when exactly.
I ...
0
votes
0
answers
58
views
How to implement state inheritance in C++ state machine framework
My first introduction to object-oriented UML statecharts was the book "Practical Statecharts in C/C++" by Miro Samek. He proposes a framework in which the state machine is a single class ...
2
votes
2
answers
144
views
Optimizing Flash Memory Writes in Embedded Systems with Unpredictable Power Loss
I'm working on an embedded C++ project that involves logging certain types of statistical data (like successful accesses, failed attempts, and other events) to the flash memory (just incrementing ...
4
votes
4
answers
282
views
Is const appropriate in this situation?
As an embedded firmware developer, I often write classes to represent/act as drivers for hardware on a microcontroller. These driver classes will usually contain pointers to hardware registers and ...
0
votes
1
answer
135
views
Is "Wrap child class operation into a function, then return the object, in order to declare most abstract type" necessary or over-engineering?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I should declare the most abstract type as possible. However, in some cases I remember, ...
4
votes
4
answers
329
views
The fastest method for removing duplicate files safety?
I have a huge amount of files (mostly documents like pdf ~80-90%, but also images, videos, webpages, audio etc.), somewhere around 3.8 millions of files which occupies ~7.8Tb of hard drive space on a ...
17
votes
8
answers
12k
views
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
I know there have been many post about diamond problem, one of it: Why do you reduce multiple inheritance to the diamond problem?. But I'm not asking what it is or what is the solution of the problem. ...
19
votes
5
answers
9k
views
How can Rust be "safer" and "faster" than C++ at the same time?
I have been told that Rust is both safer and faster than C++. If that is true, how can that be even possible? I mean, a safer language means that more code is written inside the compiler, right? More ...
3
votes
1
answer
229
views
How to implement the visitor pattern without inheritance
I write embedded firmware using C++. A common job for firmware is to "handle" different types of "messages" (e.g., in a communication protocol). The "C" way of doing ...
0
votes
0
answers
77
views
Const correct interface for non-mutating function that returns mutable references into data structure
I very much want my code to be const correct and I'm struggling to get the following case right.
Say we have a function F that takes a data structure D and computes an auxiliary data structure D'. F ...
1
vote
1
answer
72
views
How to return a result from an active object state machine
I frequently use the concept of Active Objects (https://www.state-machine.com/active-object) combined with state machines when designing code. The key idea behind these is that only "events"...
1
vote
3
answers
532
views
Is "avoid misuse in other languages" a valid reason to avoid myString=="abc" in c++?
For example, I know in c++, I can use
myString=="abc"
to check if 2 strings are equal. However, in Java, it is comparing if 2 objects are the same object. Also in other language, for ...
-2
votes
1
answer
99
views
What could be the best approach to reallocate the memory of dynamically allocated arrays? [closed]
What could be the best approach to rellocate memory for dynamically allocated array?
Say with the help of malloc we initially allocated 10 integer size memory. Now we want to change it to point to 20 ...
0
votes
1
answer
135
views
Some approach or Design Pattern to implement same method different parameters of Interface class
I am using the Flyweight pattern to cache and reuse objects of the different classes. For example, I have a Shape interface class and multiple types of Shapes implementing the methods from the ...
0
votes
3
answers
226
views
Mapping a range of values to a single value
So I have this problem where a specific ValuesA enum value is to be mapped to a respective ValuesB enum value, The trick is there can be multiple ValuesA mapping to a single ValuesB enum.
So for the ...
2
votes
1
answer
80
views
Converting the classes with multiple dimension to Bridge design pattern
Bridge design pattern is something which is very confusing and GOF does not do justice with beginners in the definition.
I was asked in my current organisation to add one more dimension to our class ...
-1
votes
1
answer
152
views
What is the best way to store a recursive data structure? [closed]
What is the best way to store a recursive data structure in a program? (in C++)
I'm reading data from file, and want to keep it in memory while I validate the data and the user can manipulate it. It ...
2
votes
3
answers
304
views
Why do we need factories in the first place?
I went through various blogs but nowhere I can find the reason of having virtual constructors in C++.
Why is virtual constructor needed and lets say if we go through the wrong way of creating virtual ...
0
votes
0
answers
35
views
Binding delegates from owner vs in component to prevent coupling
While researching the topic of Coupling, I still have a little misunderstanding about what counts as less / more Coupling. I will take as an example two options for setting delegates in Unreal Engine ...
5
votes
1
answer
112
views
Is tag dispatch as used in CppCoreGuidelines T.65 antiquated?
The CppCoreGuidelines contain the following:
T.65: Use tag dispatch to provide alternative implementations of a function
[...]
Example
struct pod_tag {};
struct non_pod_tag {};
template<class T&...
3
votes
9
answers
610
views
Can a Domain Object always be "completely ignorant of persistence" and yet still possible to persist?
Domain Object, while not necessarily a design pattern, can at least be considered as a design "concept", and one of its points is:
https://wiki.c2.com/?DomainObject
Domain objects should ...
1
vote
1
answer
159
views
Is it more secure to develop libraries in-house than using existing libraries? [closed]
I (unfortunately) work in a large German corporation, in a department where the codebase is up to 20 years old, written in C++ (actually more like C with the occasional classes, since most developers ...
2
votes
3
answers
379
views
C++ Is it okay to use nested classes as a way to namespace derived classes?
I have many abstract classes that describe many abstract ideas and objects. These classes have many complex relationships with each other, and I realize that while writing code with some of the ...
2
votes
1
answer
108
views
Functions vs Classes for delegate pattern
Please consider the following sample
#include <cstdio>
#include <functional>
#include <memory>
#include <utility>
class PaintDelegate {
public:
virtual ~...
0
votes
0
answers
56
views
Decoupling a Configuration API in an Embedded Framework
In my embedded project I am designing a framework that allows the user to create widgets at compile time. All widgets must provide the same base functionality. Further, there are multiple widget ...
9
votes
4
answers
3k
views
How to analyze and understand the use/application of a "class" in a colossal million-line legacy code base?
I am working on a huge code base (more than a million lines of code with a sophisticated architecture) written in C++ over the span of a couple of decades. The task on which I'm working at this point ...
-1
votes
1
answer
108
views
Best approach to communicate C++/Python and Front-End
I work with Computer Vision applications, mainly in Python and C++. Recently, I get involved with a customer that wants to create a UI based on React JS to visualize the results of our software in ...
12
votes
4
answers
3k
views
How to initialize the same global resources from multiple modules independently?
I encountered the following situation: I have two modules, TokenService and Wifi which are initialized from main(). The modules themselves don't know of the existence of each other and function ...
-2
votes
1
answer
202
views
How to create a cross-platform class abstraction?
I have been struggling with how to create an "object-oriented" cross-platform abstraction.
In C++, a class requires a certain amount of coupling between its interface (i.e., public methods) ...
0
votes
2
answers
156
views
What is the difference between C++ Composite and Chain of Responsibility patterns?
A Composite delegates calculations of unknown depth to a virtual method.
A Chain of Responsibility passes events to an arbitrary deep line of handlers.
Sounds the same to me. What am I missing?
2
votes
1
answer
158
views
Socket Class Design C++
I am currently trying to design some C++ classes that encapsulate POSIX sockets and their relevant functions. I want to try and design classes that are simple, elegant, and make the best use of design ...
2
votes
1
answer
651
views
Saving and reading files in user specific application data in C or C++
Most useful or reasonably complex applications need to save data such as user settings or saved games or browser history. I have been working on applications and games in C or C++ but I am not sure ...
3
votes
3
answers
299
views
How to efficiently build objects without default constructors
I am trying to explain and understand a way to create object which cannot have "default" instances. Typically I want classes which represent, as closely as possible, the real-life concept I ...
0
votes
1
answer
258
views
C++ wrapper around raw array - memory management
I have simple wrapper class around C-style array. I don't want to use std::vector since I want to have only one array even if I do copy of the struct. With std::vector the vector is also copied.
...
1
vote
0
answers
86
views
What is the best way to implement the following type of paradigm?
I have the following piece of code. Line 1 is a container (for simplicity, one can think of it as a list of elements e1, e2, ..., en). Now there is a function function_fun which takes as input an ...
-2
votes
1
answer
84
views
Support different versions of a third-party library for different OS
how to support to vendor libraries. Apart from file changes, and APIs, the new version is 64 bit whereas the old is 32 bit
What's the best design and implementation effort to support two version of a ...
0
votes
1
answer
80
views
Use PGO (profile guided optimization) to determine optimal value of variables in code
Reading this interesting paper it seems that a lot of performance loss is due to scheduling overhead in tight loops. To recap: There's a variable called "Chunksize" which determines how big ...
-3
votes
1
answer
183
views
Will anything that's written in c/c++ be rerwritten in rust? [closed]
Will anything that's written in c/c++ be rerwritten in rust?
for maintainance, even for performance?
0
votes
1
answer
144
views
Sink or non-sink API's?
This might be an opinionated question but I'm eager to hear your anecdotes as I'm at the beginning of designing a new API. Now I'm questioning whether I want this API to be a sink API or non-sink API. ...
-1
votes
1
answer
65
views
How to handle dependencies between objects/components
Im currently working on a college project on the simulation of magnetic interactions of particles, see physical simulation: design thoughts.
Right now im starting to code my system according to this ...
0
votes
1
answer
144
views
Clang compiler is unstable for me, backtrace msg asks me to open a GitHub issue. How often should I post my issue?
This question is about how to be a good citizen in an open source community, and do my part to effectively participate, to advance the project/not harass people.
I am building a medium-sized C++ ...
0
votes
1
answer
258
views
Custom #pragma directives
I'm creating a language parser on a microprocessor in C++. For the tables of keywords and commands, rather than maintaining a single curated file (alphabetically sorted, etc), I'd prefer to declare ...