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
1 answer
149 views

Should you default to using classes in C++? [duplicate]

I recently discovered the KISS and YAGNI principles which made me question my usage with classes because I use them constantly without really thinking about it and I realized my code could be much ...
Konjointed's user avatar
0 votes
0 answers
41 views

Setting Dependency Design

Hello I'm currently designing an GUI application who's main job is to update and display settings and some other data. For context, I'm using QT framework. I'm starting to refactor the settings ...
mcmatt90's user avatar
2 votes
1 answer
168 views

C++ vs C console output idioms

I'm struggling to understand pros and cons of the C++ and C approaches to console output. C++ uses the stream approach with concatenation of operator<<, while for "C approach" I mean a ...
Nicola Mori's user avatar
0 votes
0 answers
74 views

How can I better design systems and components in ECS?

I'm working on a game from scratch in C++ which is based on an Entity Component System and I've ran into a bit of a problem with the way I've been designing my systems and components. I find myself ...
Konjointed's user avatar
-3 votes
0 answers
53 views

Testing python codebase and a C++ codebase

Our company has a Python codebase which is typically used for prototyping and a C++ codebase for the actual production. The pipeline is similar and particularly the functions are expected to produce ...
SKPS's user avatar
  • 95
1 vote
1 answer
123 views

App that can be deployed both as monolith and microservices?

We have a monolith C++ application and I consider doing some major refactorings. Since we're not completely ready to go full "microservices only" (maybe in about two years time), I want to ...
glades's user avatar
  • 377
1 vote
4 answers
113 views

A method for handling many runtime configuration parameters

Suppose I am developing a class which implements some high-level application logic. I want the "configuration"/"attributes" of this logic to be modifiable at run-time (with some ...
Patrick Wright's user avatar
4 votes
5 answers
425 views

Is it bad practice to use nullptr in ternary operation?

I've set up a ternary operator in place of a pile of if-else's, the final expression being nullptr in order to finish the loop, like so: int menuSelect; std::string operation=""; (...
Hench's user avatar
  • 51
25 votes
2 answers
6k views

Why is the term "string" so often abbreviated as "sz"?

A pattern I have noticed in many big C and C++ programs - including Microsoft Windows (REG_SZ type in Registry) and Valve's Source SDK (names of practically every string variable) - is that "sz&...
AcinonX's user avatar
  • 467
0 votes
4 answers
451 views

Should I use a singleton to represent the application as a whole?

I make sure when designing software/firmware to make heavy use of dependency injection so that different pieces of the application are not directly coupled to one another. This also allows me to (...
Patrick Wright's user avatar
2 votes
2 answers
212 views

How to "explicitly" declare an implicit interface?

Abstract (virtual) interfaces allow you to explicitly declare a set of methods which a child class must implement in order to compile and be used with other parts of the code. This works great for ...
Patrick Wright's user avatar
-1 votes
1 answer
109 views

Finding intermediate type for conversion

After reading the whole question, you might think why I even need this, but believe me, it has a crucial usage in my project, which is very broad and complex, so I will only include the things ...
Aries Victor's user avatar
0 votes
0 answers
38 views

CMake Reusability and Package Management

My question is focused on CMake C++ projects and separating out code into multiple repositories for re-usability, and somewhat mimicking a naive package management system. For now I would like to ...
nick2225's user avatar
  • 147
2 votes
1 answer
149 views

Why is std::dynamic_extent not zero in c++?

For context, I'm trying to make a span type that has a size type parameter, which mimics much of the std::span api. std::dynamic_extent is not zero in C++, instead being defined as -1ull or std::...
Krupip's user avatar
  • 1,300
0 votes
5 answers
238 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
111 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
104 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
172 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
140 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,300
1 vote
2 answers
163 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
224 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
  • 377
1 vote
2 answers
341 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
  • 377
2 votes
2 answers
140 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
106 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
160 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
136 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
262 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
209 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
233 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
166 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
374 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
159 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
58 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
573 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
  • 927
0 votes
3 answers
145 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
176 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
152 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
262 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
154 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
199 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
297 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
151 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
418 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
13k 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
12k 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
374 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
84 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
155 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
583 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

1
2 3 4 5
55