Questions tagged [c++]

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

Filter by
Sorted by
Tagged with
-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 ...
niranjan kumar's user avatar
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 ...
Anon's user avatar
  • 3,593
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 ...
francesco's user avatar
  • 119
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 ...
Nicola Mori's user avatar
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() ...
CygnusX1's user avatar
  • 237
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 "${...
Krupip's user avatar
  • 1,270
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 ...
Patrick Wright's user avatar
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 ...
glades's user avatar
  • 351
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 ...
glades's user avatar
  • 351
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 ...
Patrick Wright's user avatar
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 ...
Patrick Wright's user avatar
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 ...
Aaditya Yadav's user avatar
-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 ...
RafalMaziejuk's user avatar
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 ...
RedYoel's user avatar
  • 123
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 ...
Patrick Wright's user avatar
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 ...
user366312's user avatar
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 ...
Cem Polat's user avatar
  • 127
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 ...
Oliver JT's user avatar
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/...
user avatar
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" ...
Patrick Wright's user avatar
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 ...
KetDog's user avatar
  • 101
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 ...
h22's user avatar
  • 917
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 ...
Aries Victor's user avatar
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 ...
JJW's user avatar
  • 9
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 ...
Tychus Findlay's user avatar
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 ...
Patrick Wright's user avatar
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 ...
xyf's user avatar
  • 109
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 ...
Patrick Wright's user avatar
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 ...
Dario.Casciato's user avatar
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 ...
Patrick Wright's user avatar
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, ...
wcminipgasker2023's user avatar
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 ...
YoYoYo's user avatar
  • 149
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. ...
wcminipgasker2023's user avatar
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 ...
euraad's user avatar
  • 325
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 ...
Patrick Wright's user avatar
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 ...
chrysante's user avatar
  • 127
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"...
Patrick Wright's user avatar
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 ...
wcminipgasker2023's user avatar
-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 ...
Himanshuman's user avatar
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 ...
Himanshuman's user avatar
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 ...
xyf's user avatar
  • 109
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 ...
Himanshuman's user avatar
-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 ...
masher's user avatar
  • 107
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 ...
H Kumar's user avatar
  • 31
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 ...
scydev's user avatar
  • 1
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&...
Jan Schultke's user avatar
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 ...
The_Sympathizer's user avatar
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 ...
hyperbole's user avatar
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 ...
nreh's user avatar
  • 151
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 ~...
Zoso's user avatar
  • 219

1
2 3 4 5
55