Questions tagged [c++]

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

Filter by
Sorted by
Tagged with
1 vote
2 answers
146 views

Appropriate use for the visitor design pattern

Assume I have a factory which takes in a series of bytes and outputs a pointer to a newly-created abstract message. Now, I would like to define some extensible, manageable, and clean way to "...
user avatar
-1 votes
1 answer
151 views

Conversion methods: from_xxx() or to_xxx(), is there a reason I shouldn't stick with to_xxx()?

I have a C++ library that I'm converting to Python. In the C++ library I have multiple constructors and many different types of conversion functions (think radians to degrees, and different types of ...
user avatar
  • 948
5 votes
2 answers
358 views

Why would you use 'new' and 'delete' for something that will be referenced through a vector?

I'm going through some code from this article about ECS-systems in game programming and trying to understand it, and something I'm seeing a lot is using heap memory in places where it seems like ...
user avatar
  • 217
2 votes
1 answer
103 views

How could Vulkan pNext design be implemented in a "safer" way?

Vulkan introduces a member .pNext of type void* in all its core structs allowing to create handles. This member purpose is to allow to extend the structure by passing a pointer another one. This is ...
user avatar
  • 129
0 votes
1 answer
61 views

Best Architecture to integrate a camera to a system [closed]

I'm trying to integrate a Camera to a more complex system in C++. The camera I'm using has a low-level API so I'm thinking about using a more simple interface. Basically what I'm planning to do is (in ...
user avatar
  • 113
0 votes
0 answers
45 views

Is it possible to develop a web server microservice-backend with gRpc/bRpc

I've the experience of development of web servers with Go and Python. I knew that there were some web frameworks, such as Beego, Django. I have used Nginx and Beego/Django to develop and deploy some ...
user avatar
  • 211
0 votes
1 answer
62 views

Documenting classes with doxygen (I want a birds eye view of the code but also want documentation)

When I code in plain C there is quite a beautiful interaction between headers and c files in terms of documenting the code. I tend to add the documentation on the cc files (and yes I am aware that ...
user avatar
  • 231
2 votes
3 answers
178 views

What methods are available to implement feature flags or "loadouts" for C++ projects?

I'm looking for a system to be able to configure my C++ project to compile some sections of codes and not others depending on what target hardware I'm compiling for. The area of development is ...
user avatar
  • 31
0 votes
1 answer
85 views

Is using lambdas and overload resolution a recommended way to write a visitor for a variant?

If I have a discriminated union and want to write a function piecewise, the following code works just fine, but is taking advantage of some fairly tricky (at least to me) stuff involving overload ...
user avatar
4 votes
5 answers
391 views

Single Responsibility Principle, Inheritance

I have the following interface: class IHittable { virtual Intersections intersects(const Ray & ray) = 0 } which will be implemented by various geometric objects(Spheres, Triangles etc). Now ...
user avatar
  • 151
2 votes
2 answers
376 views

Alternative to the Visitor Design Pattern

I have been trying to come up with a method to "serialize" various objects into various different formats. For example: class Shape { public: virtual std::string_view name() const = 0; ...
user avatar
0 votes
1 answer
223 views

Constantly writing a JSON file [closed]

I need to write continuously into a JSON file in C++. The format and structure are defined as requirement. The data is produced in the process itself and needs to be stored multiple times per second. ...
user avatar
0 votes
2 answers
67 views

Deserialization and routing of objects

I know this question has to have been answered in countless ways before, but I would appreciate some help pointing me in the correct direction. I am developing an embedded system that receives "...
user avatar
0 votes
0 answers
28 views

User proof network app with distant database [duplicate]

I’m currently writing a software for my company (3rd world country), and I wanted it as simple as possible to install and maintain, so I started with Qt and no server app beside the database, (MariaDB ...
user avatar
  • 159
1 vote
3 answers
268 views

Debug statements in production quality code?

Would anyone here recommend using debug statements such as the following in production quality code? I think these are personally one of the easiest to include or exclude, but they make the code hard ...
user avatar
-2 votes
3 answers
96 views

Singleton as Interface for testability via dependancy injection

It can be found in many advices on topic that having Singletons is an anti-pattern. Especially for cases of testability. Can someone please advice/critique on this way (please see code below) of ...
user avatar
  • 1
4 votes
3 answers
252 views

Should I make my class mockable by marking its methods as virtual or by creating an interface?

In the process of refactoring non-testable code we are re-designing some of ours classes so that it can be easily replaced by a stub or mock during unit tests. Here is an example of such class (...
user avatar
  • 221
1 vote
1 answer
181 views

C++ PImpl and Move Semantics

Suppose I'm writing a C++ class with the PImpl idiom for the usual reasons of providing a stable ABI and/or reducing #include dependencies. I want the class to have value semantics: modifying a copy ...
user avatar
  • 119
17 votes
5 answers
4k views

Why are C++ "complex" functions not member functions?

I was looking at the C++ library <complex>, and noticed that functions such as std::conj and std::norm are free functions i.e. static functions not placed inside the std::complex class. Why is ...
user avatar
-1 votes
1 answer
130 views

Architecture for interfacing to embedded systems hardware in c++

I have been spending quite a bit of time recently researching the "best" (read most elegant. robust, simple to use, and resource friendly) ways to develop low-level hardware abstractions for ...
user avatar
-2 votes
1 answer
179 views

Design Patterns in C++: What is the best way to implement an interface with a generic type? [closed]

I would like to use a generic interface that has been implemented. I have two classes as such: class App { MainWindow<State> mainWindow; protected: virtual MainWindow<State> ...
user avatar
  • 99
2 votes
4 answers
462 views

Confused about inheritance and correct usage. Should I avoid using members in base class?

I am using C++ , but as far as I understand most OO principles are cross language. In most of the articles that I have read and liked about inheritance the advice are about : not to use it for the ...
user avatar
0 votes
1 answer
117 views

Abstract Base Class decides which type of child class to create at runtime

I have an Abstract Base Class AbstractModel class AbstractModel { public: struct predictionStructure{}; virtual predictionStructure predict(CompanyLib::Matrix<double> data) = 0; std::...
user avatar
2 votes
2 answers
2k 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 ...
user avatar
0 votes
0 answers
44 views

Why not using .hpp files containing forward declaration AND implementation over separating to .h and .cpp [duplicate]

I am not very experienced as a c++ programmer so I may not have understood all the subtlties of what i'm doing : yet, for a few months now, I always write my modules with .h files that are only #...
user avatar
  • 109
0 votes
1 answer
309 views

How to expose C++ static library interface, extending Pimpl to an abstract interface

Until today I had a static C++ library with no separation between the public interface and internal headers. My other apps just linked to it, included the required headers, and used whatever they ...
user avatar
-1 votes
5 answers
887 views

In C++, does dereferencing a nullptr itself cause undefined behaviour, or is it the acting upon the dereferenced pointer which is undefined?

I happen to have a reason why I might want to dereference a nullptr. Of course when I do, my program crashes, and from what I gather, this is due to the compiler playing it safe and stopping my ...
user avatar
  • 3,319
2 votes
1 answer
124 views

Best practice for designing a WebAPI for exposing data in a SQL database interacting with C++ and C# applications

I am looking for general guidance or any relevant links for a complex project. We are a small business that sells a construction materials testing software. The client is a Windows application with ...
user avatar
1 vote
2 answers
81 views

API for input stream operation read until

I want to create a stream class The input stream should read/parse a continuous range from left to right providing convenience methods. The implementation isn't a problem but choosing a consistence ...
user avatar
  • 119
2 votes
0 answers
92 views

Is there an elegant way to handle run-time dependent object composition without using dynamic_cast?

Context I'm attempting to design a modelling framework that is modular and easy to extend and test. (I'm trying to follow open/closed, dependency injection, and composition over inheritence principles)...
user avatar
  • 29
0 votes
0 answers
14 views

When to create a new socket in ZMQ or add a new topic to an old one in pub/sub?

I have an application that remotely speaks to another application. There's a lot of coordination between the two applications, but they can exist on their own with out the other, just with restricted ...
user avatar
  • 948
-1 votes
1 answer
95 views

If you could define function by prepending `inline` in header file, why people recommend declare in header then define in a seperate .cpp file?

So as I was learning, I was told that it's bad to define function within header files, as if it's included in multiple places, it'll produce multiple copies of that function and later causes error in ...
user avatar
-2 votes
2 answers
246 views

Is TC++PL 4th Edition by Bjarne Stroustrup outdated?

First of all, I hope this question doesn't fall under "Asking For Books Recommendation" Category. Since TC++PL covers C++11, it is 3 revisions old (C++14, C++17, C++20), the ISO group is ...
user avatar
-2 votes
1 answer
185 views

Why aren't constructors atomic? [closed]

If thrown exceptions in constructors can lead to memory leaks or partially-constructed objects, then why we don't make them atomic? so neither an object nor it's local variables will get created/...
user avatar
  • 99
0 votes
1 answer
146 views

Asynchronous Server in C++

We are looking to develop an asynchronous server in C++. We are coming from C#, which has built-in support for async-await networking. However, with C++ it appears as if it is basically mandatory to ...
user avatar
  • 99
0 votes
2 answers
149 views

Design problem in C++

I am trying to engineer a library for the Genetic Algorithm optimization method. The main class for the GA is quite general. Here is what I have for it struct GAOptions{ size_t max_ga_steps; ...
user avatar
  • 103
-1 votes
2 answers
127 views

Hide private class member details from user but use it internally in non user facing code

I find myself in a situation with the following implementation. I have a class that is exposed to users. My intention is to expose a high-level abstraction to the user rather than internal details of ...
user avatar
  • 135
5 votes
2 answers
152 views

Finding the right abstraction and minimising invalid states

I am writing a library to allow two people to play chess for a toy project. For what is worth, it is in C++. I have deemed useful to have a class named Move, which represents a move which needs to be ...
user avatar
0 votes
1 answer
116 views

Should i overwrap functions to maintain Single Responsibility for code organisation? [duplicate]

I am trying to maintain proper code organisation for my codebase. I came across a situation whereby organising my code into class A, B, C is unnecessary as class C can be incorporated into class A and ...
user avatar
1 vote
1 answer
168 views

How does a blocking call work?

Effective Modern C++ recommends using void futures as a way to communicate one-off events from one thread to another. Below is a minimal reproduction of that practice. std::promise<void> promise;...
user avatar
  • 21
2 votes
0 answers
72 views

Integer type for data structure index

I have implemented an append-only tree data structure as a class in C++20. Internally it uses std::vector<Node<T>> where Node<T> contains an int parent_index and T payload. A node ...
user avatar
-1 votes
3 answers
268 views

Dilemma over authenticity of gcov generated code coverage percentage where unit tests are not technically correct

When I joined my company as a new comer and I was exploring the unit test suite of the product code. It is using gtest framework. But when I checked all the tests, they were testing the whole ...
user avatar
3 votes
1 answer
176 views

The downsides of enforcing the use of .at() for STL containers

One of the complaints about C++ is the lack of automated bounds checking, which lead to memory errors. But the C++ STL containers provide a quite convenient .at() method for bounds checking a ...
user avatar
  • 2,491
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 ...
user avatar
1 vote
1 answer
103 views

Where would you place the working description for a function?

i like to think out a function using comments before writing it. I'd like to keep the information in the code as comments somewhere but i don't know where it should be kept. At the moment i keep it at ...
user avatar
2 votes
1 answer
726 views

How to do MVC in C++

I have a QT application with a window of OSG written in C++. I want to implement the app using MVC design. The application allows you to add markers to OSG model and it is using QT to edit the markers....
user avatar
2 votes
1 answer
147 views

How can I enforce that decorator pattern is complete at compile time?

I have a C++ class (Class D) that is a decorator of another class (Class B). Class D inherits from B and also requires an instance of B to construct that it keeps track of. Class D overrides all ...
user avatar
1 vote
4 answers
307 views

How can composed sub-objects access the parent object?

Let's say I want to make a class for a car. A car has an engine, transmission, steering, radio, ... Each of these parts have its own internal state, so in C++ it makes sense to use composition like ...
user avatar
  • 157
1 vote
1 answer
127 views

If methods are actions that an object can perform, how do we represent abstract objects?

I have read that objects contain the actions it can perform as functions and its attributes as variables. But i am getting confused with objects that don't seem to perform any actions such as an ...
user avatar
1 vote
2 answers
119 views

using-declaration or typedef for iterator tags?

I'm creating a class representation of a mathematical tuple (or simply, an ordered set of numbers). Being a list-like object that could benefit from an iterator representation, I have decided to give ...
user avatar
  • 119

1
2
3 4 5
53