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
36
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
187
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 ...
7
votes
4
answers
36k
views
How to represent constants in UML class diagram?
I am working on developing a UML class diagram from C++ code. I have a classless header file that is basically a definitions file (it has structs, enums and defined constants). I know how to ...
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 ...
9
votes
5
answers
15k
views
How to write my own unit test?
I am currently trying to write a unit test of my own. The unit test I am writing is mainly for linear algebra (I know there are great package out there but I want to use my own for my project). My ...
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 ...
11
votes
3
answers
9k
views
What are the best practices when implementing C++ error handling?
FYI: I was asked to post this on softwareengineering - this question was originally posted on stack overflow, but they are a bit funny about broad questions over there, hence I was told to post here ...
113
votes
5
answers
14k
views
Does a `long` ban make sense?
In today's cross-platform C++ (or C) world we have:
Data model | short | int | long | long long | pointers/size_t | Sample operating systems
...
LLP64/IL32P64 16 32 32 64 ...
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 ...
5
votes
2
answers
2k
views
How to maintain encapsulation with composition in C++?
I am designing a class Master that is composed from multiple other classes, A, Base, C and D. These four classes have absolutely no use outside of Master and are meant to split up its functionality ...
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 ...
1
vote
2
answers
165
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() ...
2
votes
4
answers
617
views
C++ - Is it bad practice to use compiler specific functions?
My requirement is simple, I want to be able to count the number of bits in a number. With a little bit of research, I found that MSVC has __popcnt, GCC has __builtin_popcount and so on.
At this stage,...
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
115
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 ...
4
votes
4
answers
2k
views
How to deal with HRESULT spaghetti?
I'm currently writing an audio application using WASAPI, which is a really great learning experience, however I have this odd problem, I don't really know what to do about all these HRESULT codes in a ...
1
vote
2
answers
295
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 ...
6
votes
2
answers
5k
views
Needs (in principle) C++ parenthesis around if statement condition?
In current C++ when body of if statements contain only one command then:
Parenthesis around if condition are mandatory but block are optional. So, both examples are OK:
if ( condition ) { return 0; }...
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 ...
4
votes
1
answer
423
views
Requesting information regarding the use of MUI vs. the use of GetText for i18n/L10n of Windows desktop applications
I have spent the past two days investigating which technology I will use to support translations of text strings (as part of an overall internationalization migration) for an archaic (VC6) Windows ...
1
vote
1
answer
96
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
vote
3
answers
236
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 ...
-1
votes
1
answer
117
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 ...
2
votes
3
answers
191
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 ...
4
votes
7
answers
4k
views
How do interactions between objects work?
I am getting confused when objects interact using OOP. Here i'm trying to model the interaction between a vending machine and a coin. The coin is inserted into the vending machine.
The interaction is ...
0
votes
2
answers
228
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
1
answer
164
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 ...
4
votes
1
answer
665
views
Potential legal issues with lock-free / wait-free structures / algorithms
While researching the subject matter - lock-free and wait-free data structures - I'm seeing a worrying amount of mention of patents with this area. I've hobby-researched compression algorithms years ...
0
votes
5
answers
753
views
What programs emit C++ code and pass it to mainstream compilers?
I suspect there're programs that instead of emitting machine code instead emit C++ code and then start a mainstream C++ compiler to compile it to machine code.
I can't find any notable examples ...
12
votes
6
answers
6k
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/...
0
votes
0
answers
104
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 ...
5
votes
4
answers
3k
views
How to avoid two step initialization (C++)?
I'd like to follow the RAII(resource acquisition is initialization) idiom throughout my code but I'm also doing the template pattern where I'm developing generic versions of my classes and using them ...
0
votes
1
answer
90
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" ...
1
vote
3
answers
566
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 ...
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 ...
27
votes
7
answers
21k
views
If a variable has getter and setter, should it be public?
I have a class with a variable that is private and the class has a getter and a setter for that variable. Why not make that variable public?
The only case I think you have to use getters and setters ...
1
vote
1
answer
480
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 ...
1
vote
2
answers
1k
views
Best way to access (grand grand) parent element in gui
I'm creating a multi window gui program, in c++ with Qt Widgets. I do have many custom gui elements, which usually are c++ classes inherited from QWidget or other Qt elements. When foo is the main ...
7
votes
5
answers
15k
views
Should I use friend classes in C++ to allow access to hidden members?
Here is my situation (a simple example).
Say I have a class called HiddenData
Then I have another class called StoreHiddenData
And finaly a class called OperateHiddenData
Here's the thing, my ...
0
votes
0
answers
88
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
141
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 ...
3
votes
1
answer
248
views
Publisher-Subscriber architecture with central registry
Problem description
Publisher-Subscriber architecture with a central registry where agents can either promote their capabilities or search for a given capability.
The project must be developed with ...
0
votes
1
answer
118
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
115
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 ...
2
votes
2
answers
162
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
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 ...
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, ...