Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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
0 answers
102 views

Best way to store key-value pairs of different types in c++?

I'm reading values from a (PE) binary file. The values have a known length and position in the file and are stored with no padding (right next to each other). They store various types (int, long, ...
user avatar
  • 111
-3 votes
1 answer
86 views

How would a SRP `class Car` look like? [closed]

SRP insists that a class should have only a single a reason to change. Please guide me through the following example. class Car { drive_forward(); embark(); disembark(); fuel_up(); } ...
user avatar
  • 6,715
0 votes
1 answer
60 views

is there a two-way (bidirectional) observer pattern?

I have two classes. "System" and "Bluetooth". I want to have a really loose coherency between these two classes as they should be able to exist or not without depending on each ...
user avatar
0 votes
3 answers
180 views

Use aggregation like LSP in C++

I was reading about LSP (Liskov Substitution Principle) in a book called Clean Architecture: A Craftsman's Guide to Software Structure and Design. I have a question regarding how this would be ...
user avatar
-1 votes
2 answers
127 views

Performance hit after non-functional refactoring

I was asked to refactor some C++ code recently for the purposes of increasing unit testing coverage. The problem was that the code was tightly coupled on one compilation unit, so we had the equivalent ...
user avatar
2 votes
1 answer
114 views

C++ Abstract classes vs selective compilation

I'm trying to find a way to achieve abstraction with the lowest possible overhead. This is for an embedded system (Cortex-M4), I use the latest gcc compiler arm-none-eabi. Let's say we have a pure ...
user avatar
1 vote
1 answer
180 views

In C++, how is signed integer stored?

I'm aware that there are a few accepted implementations of how signed integers are stored, specifically negative values. There's two's complement, one's complement, and sign/magnitude. For Visual C++, ...
user avatar
  • 33
1 vote
2 answers
154 views

If an object x is an instance of a type T, then what is a type T for a concept C?

In concept-based programming (as in C++ concepts), I am wondering if there is a noun to say that: A type T is an XXXX of a concept C. in the same way we can say that: An object x is an instance of ...
user avatar
  • 159
-3 votes
0 answers
70 views

Utilizing OOP principles effectively to implement a graph algorithms demo application

I am currently working on writing a C++ app whose purpose it is to essentially "demo" various graph algorithms. At the moment, I have a very bare-bones Graph class working (which utilizes ...
user avatar
3 votes
2 answers
364 views

Testing interaction with proprietary software without a license

First a bit of context: My company develops a software and middlewares to make our software interacts with other software. One of a client has a proprietary software we should interact with, he gave ...
user avatar
  • 944
1 vote
1 answer
93 views

What is the best way to program a lattice graph?

What is the best way to program a graph like this: I know I could use Adjacency list or Adjacency matrix: //https://stackoverflow.com/questions/5493474/graph-implementation-c //Clearer's answer ...
user avatar
  • 161
-2 votes
3 answers
205 views

How to signal a dealbreaker error from a c-tor?

Context: 128kB RAM, freeRTOS. Considered solutions: Exceptions. Discouraged by both the memory size and the code style guide. Late bool init(...);. Has worked for a decade but has it's problems - can ...
user avatar
  • 6,715
0 votes
1 answer
105 views

aliases for namespaces in headers?

Everytime I write a header I end up doing something like this: #ifndef D723E2D5_1943_4166_87CC_73F5C9C47544 #define D723E2D5_1943_4166_87CC_73F5C9C47544 #include "RandomIntegers.hpp" ... #...
user avatar
  • 161
-1 votes
1 answer
108 views

Smart pointer class choice: Simplicity vs. right tool for the job

I'm wrestling with a design choice question here. I've got a class that needs a couple of semaphores. Semaphores are non-movable objects. Objects of this class however need to go into a vector (there'...
user avatar
  • 1,029
1 vote
1 answer
128 views

Free standing functions usable for Base pointers

I read multiple times that when a member function only uses API of class it is member of, then it should be made free standing and put in to same namespace, for example, bad practice: namespace A { ...
user avatar
  • 161
0 votes
2 answers
210 views

Recommended way of hiding implementation details?

I have a single *.h file. This file contains a single (more to come) function declaration. Now the implementation of that file is very complex. the corresponding *.cpp contains several function ...
user avatar
0 votes
2 answers
111 views

Accessing Hardware-based resources from multiple worker threads

I have a technical application that interacts with different hardware components: measurement devices, sensors, custom hardware. These use dedicated interfaces like USB, serial ports, TCP/IP ...
user avatar
  • 119
-3 votes
2 answers
109 views

Using templates to define sizes of containers?

Is using templates for specifying capacities of containers a good use or misue of the templates? Eg. template <unsigned int Length> class String { public: String(); String(String& ...
user avatar
0 votes
1 answer
124 views

std::string violates Single Responsibility Principle? [closed]

I recently attended a C++ talk on SOLID principles and the presenter provided std::string as an example that violates SRP. I wasn't quite able to comprehend why that was the case. From what I ...
user avatar
  • 199
2 votes
1 answer
367 views

Difference between `Class.X` and `Class.getX()`?

Might be a silly question or something I might have just messed up in my head but here we go... I saw a code example of someone using getPos() in their own class to retrieve the current position of an ...
user avatar
0 votes
1 answer
139 views

In C and C++, can a compiler store an unamed object generated from a expression, if that same expression is reused, but the result is unchanged? [closed]

I'm mostly interested in C and C++, but I think this question can also apply to other languages. My question is, if an unamed object or value is generated from the evaluation of an expression or ...
user avatar
1 vote
1 answer
56 views

How to integrate a profiler in the least invasive way possible?

I am currently using tracy to profile a program I am writing. Tracy as a tool seems quite awesome, but it has one issue. You need to markup your code when profiling. This has the negative consequence ...
user avatar
  • 241
0 votes
2 answers
156 views

Debugging to track attribute change

I have a very large C++ application running on a *nix environment, which is occasionally setting a particular date attribute on a structure, way off what it should be. I know which structure and which ...
user avatar
2 votes
2 answers
302 views

How to handle duplicate null checks

I'm working with a legacy codebase that has a lot of functions with a nullcheck on the same object. Example: std::vector<SessionNode*> * SessionManager::SessionMap; Session * getSession(int ...
user avatar
1 vote
0 answers
45 views

Create Views depending on the dynamic type of an object returned by the model in MVP

In my MVP app, my model (a service) returns a collection of ISession pointers. Each pointer points to an object of a derived type HttpSession or HttpsSession. The model (the service) knows nothing ...
user avatar
  • 111
-1 votes
1 answer
41 views

Create new object based upon another abstract object

Assume the following class structure: class Base; class A : public Base; class B : public Base; class WrapperBase; class AWrapper : public WrapperBase; class BWrapper : public WrapperBase; I ...
user avatar
2 votes
2 answers
186 views

When calculating the runtime of a c++ program, how do you factor in constexpr/tmp functions?

It's mostly all in the title. How do you report the runtime of a C++ program that has intensive calculations at compile-time? If I have a program that, when I use the bash time command on it, appears ...
user avatar
0 votes
1 answer
225 views

Why is there no Hashmap in C++ like Java? [closed]

I know that the C++ standard library includes the ordered and the unordered map (std::map and std::unordered_map) containers, but why is there no hashmap available on C++? I was asked this questions ...
user avatar
3 votes
2 answers
104 views

File scope static variables with non-trivial destructors

I am coming from the Google C++ guidelines, specifically the section on Static and Global variables. It advises against having global variables of types that aren't trivially destructible. I'm unsure ...
user avatar
  • 199
2 votes
2 answers
92 views

Design pattern to create a shared lib architecture

I am making a renderer as a hobby, one thing I thought to try is making the low levelAPI be dynamically swappable, i.e. you could have an opengl or vulkan backend and switch between the two without ...
user avatar
  • 241
2 votes
1 answer
123 views

Wrapping a legacy C++ codebase with a C API

I have inherited a large C++ codebase implementing various Windows desktop applications, services and libraries using Windows MFC. There are no automated tests. We need to decouple the UI and retain a ...
user avatar
  • 91
0 votes
2 answers
240 views

Mocking clock in C++ unit test

I'm working on a project that does things depend on how much time has elapsed. Specifically for testing purposes, I need to call a method twice a second. The basic idea of the test is something like ...
user avatar
  • 303
0 votes
2 answers
118 views

On hanging Desktop-applications

I am filled with questions pertaining to the situations where a desktop-application hangs, possibly for minutes. I've got this complicated game-engine written in c++. Several times during development, ...
user avatar
1 vote
1 answer
87 views

Constructor initializer list too bloated?

I noticed that my Member initializer list is not really readable. My main.cpp files looks like this: #include "Application.hpp" using Pathfinding::Core::Application; int main() { ...
user avatar
  • 161
2 votes
1 answer
129 views

Delegating whole class bad practice?

Suppose I have the following struct: struct Dimension { void setDimension(Vector2i dim); Vector2i getDimension() const; bool canShowInformation(); } And this struct uses the Dimension ...
user avatar
  • 161
0 votes
4 answers
108 views

API design: Should a count() of a set of consecutively-id'ed elements return a size type?

I'm writing C++ code, where the standard library has an idiomatic type for representing sizes: std::size_t. Now, I'm writing a function which counts certain kinds of objects; and these objects have ...
user avatar
  • 2,363
-1 votes
3 answers
299 views

What is the difference between Facade and God?

A Facade is a higher level API over a whole subsystem. A God is class that violates SRP. Where do we draw the line? Does the following code represent any of the two: enum UnitType{Demon, Paladin}; ...
user avatar
  • 6,715
-3 votes
1 answer
48 views

Is it possible to add a unique identifier to the shared Data via bittorrent (P2P)?

I know that P2P sharing is copying the exact content between the peers. Everyone has the same data. Let's assume the content is very important and I don't want anyone to distribute outside the group. ...
user avatar
  • 1
0 votes
0 answers
40 views

How could i reduce compilation time without using less templates? [duplicate]

I am part of a small project that is doing some research code in C++. Our work involves a lot of mathematics and due to its nature we often need to temporarily use other people's code/libraries. To ...
user avatar
  • 241
1 vote
2 answers
261 views

Unit testing of classes with functions as parameters in C++

Let's say I have a function in a class with the following signature: int fun(int x, int y,std::function<int(int, int)> funArg) The output depends on the operations done in funArg. My question ...
user avatar
0 votes
1 answer
81 views

C++ class design with shared pointers methods [closed]

I dont exactly know, how to handle the problem, where one class uses shared pointers but I want to call its method from inside other class via this pointer. class Bar { public: Bar() = default; ~...
user avatar
0 votes
1 answer
52 views

Plugin system design core component access: Common practice?

I wonder how to design a good plugin architecture in C++/Qt. The main concern I have is about dependency injection vs globals when it comes to the core components. Plugins should have access to ...
user avatar
1 vote
5 answers
277 views

The size of a byte in platform independent software

When writing platform independent software in C++, can I nowadays safely1 assume that a byte has a size of 8 bits? Or do I have to calculate it like this, for example: #include <climits> std::...
user avatar
0 votes
0 answers
48 views

Logic in lambda functions together with generic classes, unit-testing

I have a question regarding the use of lambdas in factories, together with generic/templated classes. It seems like it is possible to get carried away with lambdas and generic classes, to have too ...
user avatar
0 votes
0 answers
50 views

Strategy for logging for a low latency multi processed web server backend

I've just finished a multi processed backend framework for a web server. The web server and processes are all written in C++.The web server sends a request to a free process which serves it back as ...
user avatar
  • 117
4 votes
2 answers
254 views

Should I use classes instead of functions with a state needed for computation?

I have implemented the cows and bulls game in C++. The code: #include <cstdio> #include <cstdlib> #include <ctime> struct DigitMatches { int matches_in_right_positions; int ...
user avatar
  • 51
0 votes
1 answer
157 views

Wrapping the UI framework

I want to write an application where I would delegate certain functionalities to 3rd party libraries. To make sure the code remains modular, I want to put these libraries behind an interface so I can ...
user avatar
2 votes
2 answers
176 views

Mixing compile time condition with runtime conditions. Bad design?

On a C++ project I got an idea to mix some compile time macro with std::optional to do something like : #include <optional> struct Foobar { std::optional<int> foo; std::optional&...
user avatar
  • 944
0 votes
1 answer
137 views

Are there reasons why I shouldn't put a cpp file in my interface?

It is quite common to separate C++ code into cpp and hpp files and to define the hpp files as the interface of the code. Therefore, when you use the code as a library, you can expose the interface, ...
user avatar
  • 103
6 votes
1 answer
167 views

Specify that a method needs a mutex held when calling

Current situation Right now I have a method like Data lookupData(Key id) { std::lock_guard<std::mutex> lock(m_mutex); auto it = m_dict.find(id); if(it == m_dict.end()) { ...
user avatar

1
2 3 4 5
53