Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [c++]

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

3
votes
2answers
461 views

C++ Gof Design Patterns heavily depends on new or shared_ptr

I'm trying to learn the best practices for code design and reuse in C++ so I am going through the well known GoF Design Patterns Elements of Reusable Object-Oriented Software. I noticed that nearly ...
3
votes
4answers
206 views

Inheritance: When only some concrete objects need to share state

I have a series of classes that represent widgets in a layout system. The base class for all of these concrete widget types is Widget. If I have a Button and a Text Box, these both derive from Widget. ...
0
votes
1answer
115 views

How to manually model a UML activity Diagram to JSON with Qt C++?

Suppose I have a kind of UML activity diagram with 1-n forks and joins. The assignment of the individual nodes relative to each other is defined by an XML relationship table via UUIDs where each ...
1
vote
1answer
187 views

Binary Search Tree with ID and Duplicate Values

I'm having trouble finding resources for this implementation I'm trying to figure out. I want to save nodes in a binary search tree (self balancing) containing an ID and value struct Score { int ...
1
vote
1answer
341 views

C++ immutable struct

I would like to be able to implement immutable data in C++. In short, given a C++ object in which I would like to modify a member variable, instead of modifying that member in place I would like to ...
2
votes
2answers
322 views

Can nested loop have linear time complexity

I was going through the traditional quick sort algorithm. I had a look on the partition algorithm in a couple of places and the implementation difference was very subtle. Here are the 2 approaches: ...
6
votes
4answers
384 views

Software design meeting procedures

Elements of my team and myself are experiencing some difficulty performing design meetings. The symptoms are: We get off track easily, a combination of low understanding of the content of the system ...
0
votes
1answer
66 views

Data Structure to join points by similarity measure

I have a series of points (x,y) and each point has a colour (in the LAB colour space). I need to associate points by similar colour and then spatially. So the end result is each point becomes part of ...
2
votes
3answers
389 views

From a language design perspective, is the reference type in C++ mis-designed

Besides the fact that all primitive types of C++ are copy assignable except the reference type, it also doesn't play well with containers or any other parts of the language where copy-assignable ...
-4
votes
2answers
150 views

Can derived class be singleton? [closed]

I need to have some common functionality while the derived class needs to have singleton behavior. For example: class A { //common file functions } class B : public A { //Needs to be singleton . ...
0
votes
2answers
200 views

How do you actually access the data of individual components in an Entity-Component-System design? (C++)

I've been scouring information on Entity-Component-System designs for weeks to try to figure out how to implement it in C++, and there are lots of wonderful explanations for different aspects of it, ...
-1
votes
1answer
103 views

Differences between branching and virtual methods

I was trying to find more info on the matter but could only find this: In C++ why and how are virtual functions slower? The answer says that the virtual call "Get[s] the right function address from ...
41
votes
5answers
9k views

How do variables in C++ store their type?

If I define a variable of a certain type (which, as far as I know, just allocates data for the content of the variable), how does it keep track of which type of variable it is?
-3
votes
2answers
187 views

C/C++ code optimization without compilation

Given a simple loop in C or C++ such as the following: for (int i=0, j=0; i<5; i=i*2){ j=j+i*1; } I would like to use a tool (gcc, clang or any other) to optimize the code without actually ...
0
votes
2answers
99 views

Abstract function declaration in C++

I am a beginner in C++. I'm currently experimenting with the Chromium source code and have noticed the following: For many of the cc files, there exist an h (header) file which is imported by the cc ...
0
votes
2answers
106 views

C++, Threads, Classes and Conflicts

This is a concept related question. I'm writing a program polling for alerts on various devices, there are two different type of alerts i need to poll, reds and oranges. My approach is to create two ...
3
votes
2answers
369 views

Should I use a unique_ptr with an array type, or a vector?

I've been out of C++ for years, last time I used it was back in gamedesign before C++11. I see all these new pointer types which seem great. But I'm unsure when and how to use them. In the old days I ...
0
votes
1answer
123 views

C++11 freeRTOS questions about queue usage and object design

Background I'm writing C++ firmware with freertos to penplottingrobot. It's a makeblock XYplotter and the firmware is connecting to mDraw software for command instructions thru USB drivers. MDraw ...
22
votes
6answers
1k views

continuous integration for scientific software

I'm no software engineer. I'm a phd student in the field of geoscience. Almost two years ago I started programming a scientific software. I never used continuous integration (CI), mainly because at ...
2
votes
0answers
73 views

Dynamically load user code from a common library

My project aims to provide a common interface between multiple different HDL simulators, so users can write test benches that will work no matter the simulator used. In this situation, it is the ...
3
votes
2answers
687 views

Is the meaning of `const` still thread-safe in C++11?

I recently came across Herb Sutter's video from about how the meaning of const and mutable has changed in C++11 to mean bitwise const (and thread-safe, as a consequence) instead of the traditional ...
0
votes
2answers
97 views

Design a tree-like hierarchy of ownership classes

I have a class, call it App, that will instantiate and own a set of classes of type Agents, each of which will instantiate and own a set of Listeners. Ownership is unique and non-transferable, hence ...
2
votes
3answers
246 views

Why is the Command desing pattern useful for game AI?

I'm trying to learn the Command design pattern and apply it to the game I'm working on. First I read about the general implementation, and I feel like I understand it pretty well. Now I want to know ...
0
votes
1answer
117 views

Orchestrating Object Instantiation and Intercommunication

We have a C++ project managed by UML software, which generates code for instantiating and passing data between objects. Unfortunately, this software adds a lot of overhead to our builds and ...
2
votes
2answers
79 views

Separate variable value from error code using struct

Let's say I have a function that calculates a sensor output, all values are >=0, but also I have to detect some error situations like "NO_POWER" which I encoded with value "-65500". On my application ...
0
votes
3answers
208 views

What would be the downsides of creating a derived interface of interface?

Would there be any downsides of creating sub-interfaces for virtual member functions that have different parameter types? A drawing of this is shown in the image attached. Apple and Orange do not ...
5
votes
1answer
137 views

Optionally runtime properties

I’m looking for information on a pattern that I have used recently. The basic idea is that there is a type with a property that can either be compile-time (template parameter) or runtime (member). std:...
13
votes
3answers
811 views

Is using C/C++ macros as a shortcut for conditional compilation a good practice?

Let's say I want to have several types of output messages in my code. One of them is DEBUG, which is printed only, when the code is compiled in Debug mode. Usually I'd have to write something like #...
2
votes
2answers
282 views

Hiding raw pointers from public API but need to pass them internally - design review

I am designing and prototyping an ncurses C++ wrapper. It's a hobby project, nothing too serious. I will omit include guards and stdlib includes etc. Consider the following files: application.hpp #...
1
vote
1answer
86 views

An interface for designing Arduino code

Often I want Arduino board to control several peripherals. Writing program for all of them in one document using #define SOME_PIN 4 constructions seems pretty inconvenient to me. Especially in such a ...
-3
votes
1answer
110 views

QElapsedTimer hasExpired() - Is it one msec off?

Here is a a part of the documentation of Qt's QElapsedTimer: bool QElapsedTimer::hasExpired(qint64 timeout) const Returns true if this QElapsedTimer has already expired by timeout milliseconds (...
1
vote
3answers
533 views

Do any compilers do this optimization for virtual calls?

This just came to mind, and not really sure how to search for this. Let's say you have the following classes class A { public: virtual Foo() = 0; virtual ManyFoo(int N) { for (...
2
votes
3answers
507 views

Is inlining almost all of my C++ application's methods a good or bad idea?

I am used to D coding and Python where the class definition also contains each method definiton. So it is my preferred style of coding. I abhor having to switch to the .cpp file and use that long ...
1
vote
2answers
145 views

How can I avoid redundant object properties when using a database?

I am writing applications in C++ using an sqlite database. Suppose I have a simple user system with a user database. User Class class User { private: void LoadFromDatabase(uint32_t id); ...
0
votes
1answer
62 views

API DESIGN: Store Table of function pointers in client or dynamically in api service

FIRST AND FOREMOST: Forgive me if the following question falls into the "off topic" category of this StackExchange, due to its slightly opinion polling nature. This is not my intention, I am simply ...
0
votes
0answers
95 views

How can I gracefully handle this addition of animation?

I have the following setup in a GUI library I'm making (for fun). Ignore the colors and positions being integers, it's just to serve the purpose of illustration. // Example program #include <...
1
vote
6answers
228 views

Optimization of iterative loop with removed items

I am translating a piece of software from an old language to c++ and am currently in the optimization stage. The software performs a calculation of loads for several items in a number of timesteps, ...
3
votes
3answers
433 views

Abstract Parent and Inheritance vs. Strategy Pattern and Composition

I'm currently refactoring a class that looks (after some refactoring and very much simplified) somewhat like this: class Foo { public: Foo(bool someFlag) : m_flag(someFlag) { }; void ...
0
votes
0answers
138 views

c++ templates - combining deduction with default template arguments

I have a simple class (details here) namespace MemoizerSupport { template <typename T1, typename T2> using DEFAULT_CACHE = LRUCache<T1, T2>; } template <typename RESULT, ...
2
votes
1answer
622 views

How would you design a “multithreaded” LRU cache using C++ (unordered_map and Linkedlist)?

Please note that this is about a thread-safe LRU cache (not simply a LRU cache as specified in https://leetcode.com/problems/lru-cache/description/). It isn't a duplicate of LRU cache design question ...
15
votes
2answers
4k views

Is converting a C++ method to a C function with a pointer argument an acceptable pattern?

I use C++ on ESP-32. When registering a timer I have to do this: timer_args.callback = reinterpret_cast<esp_timer_cb_t>(&SoundMixer::soundCallback); timer_args.arg = this; Here the timer ...
0
votes
4answers
864 views

Avoiding vtable pointers in objects in C++

In my previous question, it was highlight that implementations of C++ such as GCC must store a vtable pointer in every copy of a class for each parent class which has a virtual function. So a class ...
0
votes
3answers
154 views

Implementing interface using class members

This was just a question I was wondering about in terms of best practice. I am writing a small C++ program that uses an interface to implement a function. In the class that uses the interface (i.e. ...
1
vote
1answer
548 views

Organizing Source files/classes in big projects in C++

I'm currently writing a bigger program in C++ and have reached a point were I get problems with the organization. I wonder if namespaces are a way out. I will try to explain what the program does and ...
1
vote
1answer
180 views

Remove duplicated switch statements caused by not knowing type at compile time of opencv matrix?

I have a situation that occurs 5 times in my code, and continues to grow. Basically I have the same switch statement structure every time I need to perform an operation that involves my matrix and ...
2
votes
3answers
209 views

How do I put objects of different types derived from a common base in the same array? [closed]

I have two classes called Player and Enemy both inheriting from a class called GameObject, how do I store them both in an array of type GameObject while still keeping their own unique information? I ...
2
votes
1answer
298 views

3D Rendering Engine architecture for multipass rendering

I'm implementing a simple 3D rendering engine for my game, I'm using DirectX11. I created a simple architecture for the rendering engine, with a central rendering system (RenderingSystem class, a ...
1
vote
6answers
339 views

Is it okay to have a parent class that doesn't represent an entity and doesn't have an “Is-A” relationship with its child classes?

I want to have an array that can hold elements of many classes data types, and each of these classes have a method called printInfo(). The way I can do that is by having a parent class called ...
-1
votes
1answer
55 views

Best OOP practices for object accessing “aggregate object” members?

Is there a best practice for something like this? (Example in C++) class A { public: int usefulParameter; std::vector<B*> bContainer; }; class B { public: void ...
1
vote
2answers
1k views

Calling a static method from constructor's member initializer list

I'm implementing a simple rendering system for a game engine. In my engine I have renderable entities that have a Model component (I'm using inheritance as opposed to a ECS for my engine for now, but ...