Questions tagged [c++]
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
2,732
questions
-3
votes
0
answers
37
views
Clarification on Arduino Inheritance and Polymorphism Limitations
I am currently working on an Arduino,I came across information regarding Arduino's limitations in inheritance and polymorphism. I wanted to seek your guidance and clarification on whether the ...
0
votes
5
answers
192
views
When, if ever, should a private member variable, used in one function, be made into a local static variable?
Lately, I have been playing with this idiom:
Changing a private member variable into a local static variable when:
member to singleton class used in only one function
member is mutex for a shared ...
1
vote
0
answers
104
views
Refactoring code for template class
I have a template class in c++ that depends on a few nontype template parameters. The implementation of some methods, and some field member, depend on the value of the template parameters, in a ...
2
votes
1
answer
102
views
Decorate class which inherits from concrete class(es)
In my project I have several "basic" interfaces whose behavior is fixed, i.e. the default implementation will always be good for every puropse. So I defined them as concrete classes with ...
1
vote
2
answers
167
views
Maintaining locks across abstraction
Over and over I am faced with a similar problem: I have to perform two actions that are mostly unrelated, except that they need to share a mutex lock, at least for a moment. For example:
void action() ...
1
vote
0
answers
108
views
Are absolute paths in C++ libraries okay to have if they are *generated* at configuration time from non absolute paths?
I've recently created a library that needs a non-text (but platform agnostic) resource to be used. I basically tell CMake, if this is an install interface, it's in the "${...
1
vote
2
answers
116
views
Message Dispatching: If, Visitor, or something else
I often write code to implement the dispatching and handling of "messages." My requirements are usually:
I should be able to add new types of messages AND/OR new types of handlers without ...
1
vote
2
answers
206
views
How to best obfuscate a built-in key in an application?
We're building an application that needs to log into a website using built-in credentials. It's not optimal to say the least, but we're stuck with "knowing" the username and password ...
1
vote
2
answers
298
views
Most efficient mapping of pixel to colors with colormaps
I'm working on a module that handles colormaps and I want to make the mapping of pixel to colors as efficient as possible. It is a performance critical section of our app. Our current solution works ...
2
votes
2
answers
130
views
How to protect class invariants when using the extraction operator?
The C++ "standard" method for serializing and deserializing a data type is to use streams with the insertion (<<) and extraction (>>) operators. This has some flaws, but it does ...
1
vote
1
answer
101
views
Virtual Method vs. Abstract Class Member
Suppose we are writing an interface class which has some "properties" and "operations." Assume also that every concrete implemented of the interface must contain the same set of ...
1
vote
1
answer
101
views
Unordered Map with indexing and range based loops [closed]
I am creating a C++ library (more of a passion project to be honest). Its aim is to enhance the existing unordered_map STL by giving it indexing abilities and range-based loops, based on the order of ...
-1
votes
1
answer
118
views
Should the logging utilities be exposed as public API?
In my library I've got a Logger class that is a wrapper around spdlog, that I use internally for logging. Logging is enabled by default in Debug builds and disabled in Release builds, moreover user ...
1
vote
3
answers
239
views
Refactoring a "filter" design
I need to refactor a class that became too messy. The class has a method that reads some raw data from one of our repositories, and another that "filters" it:
The data is in the form of ...
2
votes
3
answers
193
views
How to create a shared driver?
As an embedded developer, I often write drivers for hardware (though this question really applies to any shared resource). The "standard" interface I have come up with looks like the ...
0
votes
2
answers
229
views
How can I purge c++ source code?
Suppose there is a C++ source code base of millions of lines composed of several hundred *.cpp and *.h files.
There is also a driver program main.cpp that uses several header files from the above ...
0
votes
0
answers
105
views
Porting a 32-bit Win32 DLL driver to 64-bit when a 64-bit DLL is unavailable in C++
I have a legacy 32-bit Win32 DLL driver that interfaces with a test equipment. I possess the API header file for this driver, and I dynamically load the DLL at runtime within my C++ test application ...
4
votes
1
answer
363
views
How to bind C/C++ functions for my language [closed]
I'm making an interpreter (currently in python but later I'll remake it in C++) and I wondered how I could use a C/C++ function in my language so when somebody wants to write an extension for my ...
14
votes
6
answers
6k
views
Why is it necessary to mark classes as not inherited from? Can't an optimizer automatically detect that virtual calls are unnecessary?
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/...
0
votes
1
answer
91
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
54
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
483
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
89
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
143
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
121
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
116
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
168
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
109
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
166
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
289
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
146
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
353
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. ...
20
votes
5
answers
10k
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
304
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
79
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
97
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
568
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
155
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
300
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
349
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
81
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
201
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
320
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
36
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
146
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&...
4
votes
9
answers
730
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
161
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
601
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
125
views
Functions vs Classes for delegate pattern
Please consider the following sample
#include <cstdio>
#include <functional>
#include <memory>
#include <utility>
class PaintDelegate {
public:
virtual ~...