Skip to main content

Questions tagged [c++]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
40 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 ...
grjj3's user avatar
  • 109
1 vote
1 answer
78 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 ...
Emile Papillon-Corbeil's user avatar
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. ...
wcminipgasker2023's user avatar
2 votes
1 answer
94 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 ...
Zoltán Várnagy's user avatar
5 votes
1 answer
500 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 ...
Konjointed's user avatar
0 votes
2 answers
134 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 ...
Karanveer A's user avatar
5 votes
3 answers
485 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 ...
Dr Phil's user avatar
  • 159
2 votes
3 answers
170 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 ...
Tomáš Viks Pilný's user avatar
2 votes
1 answer
136 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 ...
Michael John McAdam's user avatar
0 votes
1 answer
68 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 ...
ManuelSchneid3r's user avatar
1 vote
1 answer
122 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-...
user129393192's user avatar
4 votes
4 answers
989 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 ...
wcminipgasker2023's user avatar
0 votes
2 answers
157 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. ...
Harshvardhan Singh's user avatar
0 votes
1 answer
139 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. ...
user129393192's user avatar
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 ...
AcinonX's user avatar
  • 613
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 ...
sayanel's user avatar
  • 399
2 votes
2 answers
345 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 ...
cuppajoeman's user avatar
1 vote
1 answer
177 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#...
Oodini's user avatar
  • 121
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 ...
Konjointed's user avatar
0 votes
2 answers
220 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 ...
Mahesha999's user avatar
0 votes
1 answer
139 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;...
Abyx's user avatar
  • 1,455
1 vote
2 answers
272 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 ...
Gareg's user avatar
  • 21
0 votes
2 answers
247 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 ...
Reverent Lapwing's user avatar
1 vote
0 answers
193 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 = &...
Wesley Jones's user avatar
-3 votes
1 answer
186 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
47 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
226 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
91 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
1 vote
1 answer
139 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
  • 419
1 vote
4 answers
133 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
5 votes
5 answers
656 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
  • 61
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
  • 613
1 vote
4 answers
694 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
244 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
116 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
1 vote
1 answer
67 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
  • 157
2 votes
1 answer
171 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
282 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,613
1 vote
0 answers
120 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
109 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
186 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
  • 247
1 vote
0 answers
183 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
238 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
249 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
  • 419
1 vote
2 answers
385 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
  • 419
2 votes
2 answers
151 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
108 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
224 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
159 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
289 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

1
2 3 4 5
56