Questions tagged [c++]

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

Filter by
Sorted by
Tagged with
5
votes
2answers
150 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 ...
0
votes
1answer
112 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 ...
1
vote
1answer
141 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;...
2
votes
0answers
69 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 ...
-1
votes
3answers
226 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 ...
3
votes
1answer
168 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 ...
4
votes
7answers
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 ...
1
vote
1answer
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 ...
2
votes
1answer
434 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....
2
votes
1answer
131 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 ...
1
vote
4answers
198 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 ...
1
vote
1answer
118 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 ...
0
votes
2answers
100 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 ...
7
votes
4answers
610 views

Why does the C++ standard still allow uninitialized primitive variables?

If reading uninitialized memory is undefined behavior anyway, why has the C++ standard not been changed so that objects of primitive type (int, unsigned, float, double, bool, char) get default-...
9
votes
3answers
2k views

Is it best practice to define a member function directly in a class?

I'm a beginner in C++, and I was wondering if it is best practice to define a member function directly in a class, such as: // something.hpp class C { inline int func() { return ... ; } } rather ...
-3
votes
1answer
116 views

Techniques I can use to optimise my C++ code further? [closed]

I'm working on loading PNG files. I know there are existing libraries, but I'm doing this for learning purposes. Previously, I was using LodePNG, which is a great library that performs really well. I ...
0
votes
1answer
83 views

Designing a library API to allow for interactivity

The application I'm currently working on is deeply coupled to the UI framework in use (Qt at the moment). I would like to separate the UI-specific code from the rest as much as possible, with the goal ...
1
vote
0answers
116 views

Scalable architecture design for managing data flow in C++ using an Observer design pattern

What I need I need a suitable and scalable solution to manage data flow between the classes in my C++ program. Why I need it Upon new feature requests, I'm continually adding new classes to the ...
1
vote
4answers
2k views

C++: Is a pointer to a vector going to cause memory issues?

I started to write a function which had a pointer to a vector as a parameter so that it could modify that vector to output results (as the actual return value was an error code), when I started to ...
0
votes
1answer
140 views

Wrapping C API with array in C++

I want to wrap a C library, libcookie, in C++. Setting The library has an object-like structure like so struct Cookie; typedef struct Cookie COOKIE; int CookieGetTemperature(COOKIE*); // ... COOKIE* ...
0
votes
1answer
201 views

C++: Broadcast/observer pattern with polymorphic events

I'm currently trying to implement an event-system following a broadcast/observer scheme. I have events/notifications that contain information about what is happening: class Event { public: Event(...
-4
votes
3answers
208 views

Confused between HAS-A and representation [closed]

I'm currently working on a game in which a maths module is being used, here is one of the classes called Vertex. I'm getting confused with the relationship between HAS-A and objects representation. I ...
1
vote
2answers
220 views

Efficient set method in run length encoded C++ map

I have been designing a C++ class that maps an integer to an integer using a vector. Because there is a lot of repetitive data (for example 11115555666222), I am using a compression scheme where I am ...
2
votes
1answer
119 views

Resolving lambdas that depend on each other

In the C++20 codebase that I work there are a couple of functions which have helper functions defined as lambdas. I can see the idea of locality for these helpers. By having them there in the function,...
2
votes
1answer
275 views

In C++, If a member function can be made static with no change to functionality, are there any performance or memory benefits from doing so?

A very straightforward question. When I think about many of the member functions I create for my classes, many of them can be made static without affecting any functionality whatsoever. If I do so; ...
1
vote
2answers
165 views

Can you define node pointers in a base binary tree class?

I've created two separate binary tree classes, with some shared functions/variables and some that are not shared. So I have tried to abstract away the similarities in a base BinaryTree class. class ...
-1
votes
1answer
61 views

How should a client handle receiving data from a seperate thread?

There are plenty of tutorials on the internet on how to build a server that handles its clients through the creation of seperate threads, but there is no tutorials (at least I couldn't find any) on ...
-1
votes
2answers
106 views

How to deal with classes with same purpose from different libraries?

I have a question regarding software design/architecture. Are there any best practices or design patterns regarding the usage of classes with the same purpose from different libraries? Let's imagine ...
-1
votes
3answers
260 views

Does wrapping functions/'things' in classes reduce efficiency?

I was reading some C++ object-oriented programming notes that mentioned that we should avoid wrapping functions in classes if it is not required, since wrapping 'things' in classes would reduce ...
1
vote
1answer
316 views

Best way to access (grand grand) parent element in gui

I'm creating a multi window gui program, in c++ with Qt Widgets. I do have many custom gui elements, which usually are c++ classes inherited from QWidget or other Qt elements. When foo is the main ...
1
vote
0answers
175 views

How to use polymorphism in embedded software?

I have been developing a control software in C++. From the high perspective the control task can be basically divided into several subtasks calculate unmeasurable physical quantity based on dynamic ...
0
votes
1answer
61 views

Is background long running thread a good choice for disk IO?

I am building a storage engine software that allows concurrent data writing, now I have two different choices here: Method 1. Background Long-Running Thread Multiple user threads write to their own ...
1
vote
3answers
184 views

How to create uniform interface of C++ classes without virtual methods?

I have been developing control software in C++. My hardware consists of a microcontroller with an integrated a/d converter and an external on board a/d converter. Both of these a/d converters have ...
0
votes
1answer
238 views

Secure IPC method for C++ in Linux

I just can't choose a method of interprocess communication that would fit my goals, so I ask for help. My list of needs is as follows: It must be safe. Communication should not be hacked. ...
-3
votes
1answer
146 views

Which non-standard C features can I use? [closed]

C and C++ have standards, but support isn't perfect, the only available copies on the internet are drafts, and there are immensely useful things that aren't standard, such as __attribute__((cleanup)). ...
1
vote
0answers
105 views

Is using C++ Classes to handle commonly used parameters a misuse of classes?

My team works on an HTTP web server in C++. The codebase has aged over time, and has a widespread problem of 12+ parameters being passed to every function. A fake example: We need to build a Car, but ...
-2
votes
2answers
77 views

Prefer passing class or variable as const reference

In my company's code base there are examples of methods which receive some protobuf messages and use them in this fashion: void doSomething(const A& a) { // Only data from a.info() is used in ...
-1
votes
1answer
86 views

How to avoid context objects and designing "UI states"

Note: I'm using Dear IMGUI, OpenGL and C++. I have a UI, it has two "modes" one where it records and displays images and information about a recording device (IE camera) and another where it ...
-1
votes
1answer
147 views

How to avoid cyclic dependency in UI application

I'm developing an UI application where I ran into an issue with a cyclic dependency. Here is the simplified code, to explain the problem. #include <list> class UiStyle; UiStyle* CreateStyle(); ...
0
votes
2answers
246 views

Approaches of splitting the same type of work among multiple threads in C++

I have a reinforcement learning project. For this I created a vectorized environment in C++, which is a handler for multiple instances of a simple game. It is highly parallelizable. Each worker can ...
2
votes
1answer
298 views

Alternatives for index arrays

I work on a C++ project where I am not really happy with the data structures. The question isn't that specific to C++, I think that I would face a similar issue in say Java or Python. There are data ...
1
vote
5answers
183 views

How to write retry code when failure is inidicated by throwing an error?

Assume I have an API that indicates failure by throwing an error (because errors are not expected). In this situation, how should retry code be written? My first thought was to use something like this:...
0
votes
2answers
129 views

Template argument type exclusion: Would this make for a useful C++ feature?

When using templates we can have T be any type upon class instantiation. If T is a specific type that needs to be handled differently or in a special way we can specialize or partial specialize that ...
2
votes
1answer
158 views

Serialization and SOLID principles

I have several configuration structures that can be JSON serialized and deserialized as follows: namespace config { struct Foo { std::string hostname {}; std::string ip_address{}; ...
30
votes
3answers
5k views

Definition of "functor"; Haskell vs. C++

I'm trying to understand whether the Haskell and C++ communities mean different things by the word "functor", or if there's some underlying concept that unifies the two meanings. My ...
-2
votes
1answer
96 views

How to manually delete a std::vector in c++? [closed]

I have following code snippet void func(int row,int n,std::vector<int>& buffer){ if(row>n){ return; } std::vector<int> new_buffer; for(int elm : buffer){ ...
9
votes
4answers
2k views

Represent individual object as a class?

What is the best way to model the objects in my OOP program? I understand that the classes represent not particular objects but "groups"/"generalizations" of objects. But, I need ...
3
votes
3answers
513 views

True cost of virtual dispatch in C++ - when stop using it?

What I've learned so far as a programmer has lead me to think that the easiest way to write large scale software is to use a lot of interfaces - this makes things very easy to isolate and test. In ...
0
votes
1answer
104 views

How to Unit test / design differently a complicated free function

I have written a Command Line Interface, where the user has to construct an object basically by providing input to a bunch of questions. I have a hard time testing these functions as there is too much ...
2
votes
0answers
82 views

How to design software driver for the analog to digital converter?

I have been developing software driver for the analog to digital converter in C++. The a/d converter is primary intended for conversion of the temperature signals. The temperature signals are then ...

1
2
3 4 5
52