People who code: we want your input. Take the 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
2
votes
1answer
111 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
95 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 ...
1
vote
1answer
169 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
0answers
29 views

Correct semantics and naming of non-member functions for class [duplicate]

I'm thinking about changing some member functions in classes into non-friend non-member functions. (partly, motivated by writings like this https://www.drdobbs.com/cpp/how-non-member-functions-improve-...
2
votes
1answer
118 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
148 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
98 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
80 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
534 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-...
8
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
105 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
81 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
85 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
335 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
94 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
100 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
205 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
136 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
112 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
133 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
135 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
57 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
104 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
209 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
84 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
140 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
56 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
160 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
132 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
134 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
93 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
73 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
76 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
140 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
108 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
210 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
165 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
122 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
129 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
81 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
276 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
97 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
76 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 ...
-4
votes
1answer
170 views

How do I manage multiple nested for-loops without using multiple variables?

If I have code that looks like this: int i; void functionA (){ for (i=0; i<10; i++){ functionB(); } } void functionB (){ for (i=0; i<20; i++){ doSomething(); } } ...
-5
votes
2answers
77 views

Library protection in C++

We have developed an algorithm that processes some data at runtime and is slight efficient in terms of time and computing compared with other solutions out there. Though we sure anyone can develop a ...
3
votes
2answers
270 views

Most expressive way of allocating member data, per Stroustrup

I was watching a lecture by Bjarne Stroustrup from 2014, on "The Essence of C++". About halfway through he rather forcefully recommends not using new, delete, or pointers for class member ...
9
votes
5answers
3k views

C++ “Zero Overhead Principle” in practice

As an exercise in code architecture, I was writing a C++ wrapper for a window library called GLFW3. In this library, when a window's X button is pressed, you can register a callback that reacts to ...

1
2 3 4 5
51