Questions tagged [c++]
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
2,769
questions
0
votes
1
answer
83
views
Which approach do I choose for representing objects and scenes in my 3D drawing library?
I'm creating my own drawing library in C++ to provide shared rendering code for my projects.
Since the library is designed to be used as a component of other projects, the renderer's representation of ...
1
vote
3
answers
208
views
"use auto" and "declare most abstract type", which guideline has higher priority?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare most abstract type when possible, for example, suppose I'm using an UI ...
5
votes
2
answers
695
views
Is "using active record pattern" a reason to inherit from standard container (eg:vector)?
According to Is it bad practice to use Inheritance to associate methods with a basic container?, I know it is bad to inherit form std containers, mainly because std containers are not designed to be ...
12
votes
7
answers
4k
views
Immutability across programming languages
I'm quite confused about the concept of mutability (or mutation?), especially across different languages. In a language like Python:
x = 10
x = 20
I believe this is called shadowing (or rebinding) ...
1
vote
2
answers
259
views
OOP Design of a Mathematical Group
A Group is formally defined as a set of Element's,
over which a single operator mul is defined.
There are different kinds of Groups,
and each kind requires a different set of parameters.
Operator mul ...
15
votes
6
answers
9k
views
Unstable output C++: running the same thing twice gives different output
So, the problem:
When I run the same C++ code in Visual Studio, with the same input and parameters, I get either the correct output, or an output that is completely messed up (99% of values go to zero)...
4
votes
1
answer
166
views
Having a collection of pointer that remove destroyed elements
Let's say I have an object Foo that uses another object Bar.
Many different Foo could use the same Bar.
When no Foo references a Bar, this Bar is useless and should be destroyed.
Clearly, that's a ...
2
votes
2
answers
279
views
Optimal way to share data between different classes
I have a C++ code that performs simulation of a physical system which deals with motion of objects. It has the following classes:
Class Main, containing all the main calculation methods and the data ...
1
vote
1
answer
106
views
How to Handle Additional Parameters in State Transition for State Pattern in C++
I'm implementing a state pattern in C++ with a context and several states. Each state implements its transition. Here's a simplified version of what that design could look like:
class Context;
class ...
9
votes
4
answers
2k
views
Is the inability to find code by searching for a class name a reason to avoid using auto in c++ variable declarations?
According to https://softwareengineering.stackexchange.com/a/180616/432039 suggested, I know the answer advised "auto" should be used instead of the actual type when declaring variables.
...
2
votes
1
answer
120
views
C++: Good approach to handle libxml2 resource management in a wrapper
I try to write a C++ wrapper to a well-known C library, libxml2. In libxml2, an xmlDocPtr represent an XML document and xmlNodePtr represents a node. An xmlDocPtr contains a root xmlNodePtr and every ...
5
votes
1
answer
535
views
Confused on how abstraction and encapsulation is helpful
Using assimp I've created a function to load 3D models and it does everything I need and I don't plan to use another library or write something custom, however, I am curious how techniques such as ...
0
votes
2
answers
152
views
Why do you need to use pass by reference in C++ to change the value of the arguments inside the function?
I'm new to coding and am currently trying to learn C++ myself. I just learned about function parameters and pass by value vs pass by reference. Everywhere I read, they say that one of the reasons pass ...
5
votes
4
answers
1k
views
C++ and memory safety
At my tiny org, the core engine is C++ due to a compute heavy workload. I need to internally react to NSA's recommendation about replacing C++ with a memory safe language.
They're highlighting the ...
2
votes
3
answers
190
views
How to decouple spagheti code for unit tests [duplicate]
A little background on the project: we as a company receive a spaghetti source code, and into that we add even more spaghetti code. So with that I want to say that
complete restructuring and ...
2
votes
1
answer
155
views
Embedded C++ classes vs. namespaced free functions
I am using C++ in an embedded environment (ESP32) and have been playing around with different ways of structuring my program. The program isn't super complex but not so simple that it could all be ...
0
votes
1
answer
88
views
Application plugin interface design
I build a plugin based Qt application which currently lives entirely in a library. The app as well as the plugins link to the library. The ::main(…) is just a tiny two line bootstrapper calling a ...
1
vote
1
answer
130
views
Do you re-include system headers in the implementation files?
Here's a scenario:
suppose you have #include <unordered_map> in the header
and you don't declare it anywhere in the implementation file
but you use it, maybe
mp[whatever] = some;
should you re-...
4
votes
4
answers
1k
views
Is updating a macro value in the Xcode preprocessor's macros violating the open–closed principle?
For example, for some Xcode projects, if I have some places that defines a number at some .cpp files:
const int PAGE_MAX=5;
and a new requirement comes that needs to change PAGE_MAX, I need to modify ...
0
votes
2
answers
218
views
Use case comparison for std::optional vs default argument
Wanted your opinion on which way will be better. Below is a sample code where I have used std::optional in the argument of a lambda and the lambda itself is an argument of the class constructor.
...
0
votes
1
answer
149
views
Is it good practice for object APIs to be required to be called in sequence to gather information?
I had this discussion with someone and it ended ambiguously.
Suppose you have a class that needs to Parse a file to gather some information. It can expose this information to you after the fact.
...
13
votes
6
answers
10k
views
Why use a special "Name" class (instead of just a string) for representing object names in C++?
Suppose we have an Instance class in a C++ program, which has a GUID/UUID, name, parents, children, and other properties which can be saved to or loaded from an XML file.
The intuitive approach for ...
29
votes
14
answers
10k
views
Avoiding throw because we are not sure the exceptions will always be caught
I'm a junior in my company, and one of the coding rules they have is:
"a constructor object must never fail" (i.e., never throw). So what if I give them an invalid parameter? Then, the ...
2
votes
2
answers
365
views
Game Systems Interaction Design
Intro
I'm writing an FPS game in c++. There is a timed game mode, players run around a map shoot from a variety of weapons which are either hitscan or projectile based, when a shot connects, based on ...
1
vote
1
answer
211
views
Using templates without generic implementation
Is it a good practice to declare a template class, but only with specialized implementations ?
Ie, there are many CRC flavours (see https://en.wikipedia.org/wiki/Cyclic_redundancy_check#...
5
votes
4
answers
3k
views
Should I initialize third party libraries in a class or function?
I'm making a game using C++ and a handful of libraries like SDL2 and OpenGL. For a lot of these libraries, you need to initialize or set them up, and there's usually some important object(s) that come ...
0
votes
2
answers
234
views
How to deal with boolean or enum variables used to decide code flow?
I am working on algorithm implemented in C++ that maintains several enum types. Say 3 to 4 enum types each with at least 4 different values. Plus the code maintains several boolean variables. The code ...
0
votes
1
answer
144
views
Error codes in a legacy C++ project [closed]
Background
I have a large C++ project which uses system error codes from errno.h, in C style.
int Cls::foo(A arg, O* out) {
if (!validate(arg)) return -EINVAL;
if (!out) return -EINVAL;
return 0;...
1
vote
2
answers
289
views
Combining Command and Visitor design patterns
Designing the architecture of a personal project, I've come up with the idea of using the combination of these two patterns to solve an architectural issue. In an MVC context, I need to implement ...
0
votes
2
answers
348
views
Is it considered a code smell to not delete static pointers of a singleton?
I have a singleton that needs to be initialized at the start of the program and it will live on until the end of the program. As is usual in this pattern, a function initializing the singleton creates ...
1
vote
0
answers
218
views
Implementing a memory efficient Abstract Syntax Tree
I am writing a compiler in C++ 20. I am looking for ways to improve heap memory performance. For example, I can compile a 36.8 MB source file that is just many repeating lines of:
let x0: string = &...
-3
votes
1
answer
193
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 ...
0
votes
0
answers
48
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 ...
2
votes
1
answer
243
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 ...
0
votes
0
answers
95
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 ...
1
vote
1
answer
148
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 ...
1
vote
4
answers
156
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 ...
5
votes
5
answers
746
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="";
(...
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&...
1
vote
4
answers
821
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 (...
2
votes
2
answers
260
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 ...
-1
votes
1
answer
119
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 ...
1
vote
1
answer
72
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 ...
2
votes
1
answer
179
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::...
0
votes
5
answers
294
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
125
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
111
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
196
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
197
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
296
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 ...