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
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 ...
AcinonX's user avatar
  • 623
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 ...
wcminipgasker2023's user avatar
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 ...
wcminipgasker2023's user avatar
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) ...
Ghassen's user avatar
  • 129
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 ...
Mahyar's user avatar
  • 11
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)...
babipsylon's user avatar
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 ...
sayanel's user avatar
  • 447
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 ...
grjj3's user avatar
  • 131
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 ...
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
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 ...
Zoltán Várnagy's user avatar
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 ...
Konjointed's user avatar
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 ...
Karanveer A's user avatar
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 ...
Dr Phil's user avatar
  • 159
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 ...
Tomáš Viks Pilný's user avatar
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 ...
Michael John McAdam's user avatar
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 ...
ManuelSchneid3r's user avatar
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-...
user129393192's user avatar
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 ...
wcminipgasker2023's user avatar
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. ...
Harshvardhan Singh's user avatar
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. ...
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
  • 623
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
  • 447
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 ...
cuppajoeman's user avatar
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#...
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
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 ...
Mahesha999's user avatar
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;...
Abyx's user avatar
  • 1,445
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 ...
Gareg's user avatar
  • 21
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 ...
Reverent Lapwing's user avatar
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 = &...
Wesley Jones's user avatar
-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 ...
Konjointed's user avatar
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 ...
mcmatt90's user avatar
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 ...
Nicola Mori's user avatar
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 ...
Konjointed's user avatar
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 ...
glades's user avatar
  • 419
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 ...
Patrick Wright's user avatar
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=""; (...
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
  • 623
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 (...
Patrick Wright's user avatar
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 ...
Patrick Wright's user avatar
-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 ...
Aries Victor's user avatar
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 ...
nick2225's user avatar
  • 157
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::...
Krupip's user avatar
  • 1,300
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 ...
Anon's user avatar
  • 3,613
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 ...
francesco's user avatar
  • 119
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 ...
Nicola Mori's user avatar
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() ...
CygnusX1's user avatar
  • 247
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 "${...
Krupip's user avatar
  • 1,300
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 ...
Patrick Wright's user avatar

1
2 3 4 5
56