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

learn more… | top users | synonyms

2
votes
1answer
70 views

Strategy for Binary File Format Description to C++ Implementation

I am dealing with a lot of legacy, reverse engineered binary file formats, often with lost source code and reading/writing these files needs to be recoded in C++. I am wondering if there are good ...
0
votes
3answers
291 views

If of loops or loop with if

Assuming the compiler is smart enough to only evaluate the if in the second example once, which version should one (semantically) use and why? if (b) { for (auto a : as) { foo(a); } } else { ...
1
vote
0answers
59 views

Pointers on porting a c++ workflow domain specific language to javascript

I am writing a game in c++ with the v8 javascript engine embedded in it. I have a c++ workflow system I use to tell things how to do specific things. It's nice and I like it, but I need it to work ...
2
votes
1answer
100 views

Writing a non-monolithic database wrapper

I have a SQL database to which I store and from which I read several objects into my C++ code. I wrote a single database wrapper which handles all the reads and writes to the DB. Of course this ...
-1
votes
1answer
144 views

How to signal a loop to continue the execution instead of using sleep

I am trying to create a thread that will sit and wait to detect new objects in a vector (the queue). Using a loop will crush the resources on the given machine, so I am trying to figure out how I can ...
-5
votes
1answer
244 views

Why code in C instead of C++ [closed]

Why code in C? Isn't C++ basically C but with better features? Why do people still use C? Would it be easier to make a game with C or C++?
4
votes
7answers
648 views

C++ delete vs Java GC

Java garbage collection takes care of dead objects on the heap, but freezes the world sometimes. In C++ I have to call delete to dispose a created object at the end of it's life cycle. This delete ...
48
votes
8answers
14k views

What happens to garbage in C++?

Java has an automatic GC that once in a while Stops The World, but takes care of garbage on a heap. Now C/C++ applications don't have these STW freezes, their memory usage doesn't grow infinitely ...
1
vote
1answer
58 views

Chain accessors or return the value of private pointer

hopefully the title isnt too confusing, I am having trouble thinking through what best-practice dictate I do in this situation. Say I have the following class structure Class A { public: bool ...
1
vote
1answer
162 views

is this a good design

For example, I have a clan and a character. There's a character that is the leader. To give the clan a specific feature, some money from the character is required. I don't want to have too much tight ...
1
vote
1answer
107 views

Using asynchronous calls for heavy IO operation: How to prevent crushing the CPU?

I have an application that reads a large binary file (1GB on average) and compresses into a bzip2 archive. I started out at first compressing these files synchronously, as I didn't want to impede ...
4
votes
1answer
179 views

Potential legal issues with lock-free / wait-free structures / algorithms

While researching the subject matter - lock-free and wait-free data structures - I'm seeing a worrying amount of mention of patents with this area. I've hobby-researched compression algorithms years ...
1
vote
3answers
167 views

How do you quickly test C++ code?

I'm working on a series of small projects (~1000 LoC each) on my own in C++. My background is in Python, where I'm used to being able to test out small changes in my code in by loading it into a ...
0
votes
2answers
92 views

What are the prerequisites to start with OpenCV in C++?

I'm an intermediate C++ programmer. Do I need any math knowledge in order to build computer applications using openCV?
0
votes
0answers
51 views

Should frequent Fonts, Colors, Pens etc be “Cached”

I am creating a game, currently everything redraws every 50ms, and that involves quite a few Gdi Objects. Here: Gdiplus::Pen outlineScrollPen ( Color (200, 0, 0, 0), 4 ); ...
1
vote
2answers
110 views

Ideal OOP Design

After learning OOP design then I got to know my programming way was not correct. One should convert physical entities or logically separable components into classes which are reusable and have their ...
4
votes
5answers
210 views

How to store math expression in c++ list

I am parsing a infix math expression to a postfix form. I want to store it in a list like [4.5, 3, 0.25, +, -] so that I can process it once it's parsed. I could store it in a string again, but I ...
0
votes
3answers
136 views

A few questions about initializing variables

I'm in an introductory programming class and we're only in lesson two, so try to keep terminology simple, ha. I'm a bit confused about when you need to and when you don't need to initialize a ...
31
votes
12answers
4k views

Is it a code smell to store generic objects in a container and then get object and downcast the objects from container?

For example, I have a game, which has some tools to increase the ability of the Player: Tool.h class Tool{ public: std::string name; }; And some tools: Sword.h class Sword : public Tool{ ...
0
votes
2answers
185 views

Compiling source code using c++

Last year, in university we used the library SDL to create a game from scratch. So I though of developping a game engine based on SDL. I will be using Qt to do the UI and SDL to implement the core of ...
4
votes
0answers
116 views

Does it make sense to apply the principles in Redux to OO languages?

Redux is essentially functional programming, and it makes a lot of sense. As I move back to a non-javascript OOP language in a different project I would like to apply the same principles such as ...
122
votes
15answers
30k views

Why do people use C if it is so dangerous?

I am considering learning C. But why do people use C (or C++) if it can be used 'dangerously'? By dangerous, I mean with pointers and other similar stuff. Like the Stack Overflow question Why is ...
1
vote
2answers
63 views

Installing multiple versions of software with git

I'm really struggling to understand git, so here's a tighter rephrasing of a previous question I had asked. I'm going to explain a hypothetical scenario, detailing what I think happens when you ...
2
votes
3answers
214 views

Why is the next element of an int array stored 4 bytes after the previous one?

I have this small code: int* array_int = new int[10]; array_int[0] = 1; array_int[1] = 2; array_int[2] = 3; array_int[3] = 4; array_int[4] = 5; array_int[5] = 6; array_int[...
1
vote
3answers
204 views

Define C++ class in one or more files

When creating a C++ class what is best practice> Put the entire class definition and member functions in a header file Put the class definition and function declarations in the header file and put ...
2
votes
0answers
82 views

Using and designing asynchronous APIs with naturally synchronous parts

I've been programming for a long time, but very rarely with anything asynchronous (and not often with anything to do with multithreading, either). Mostly for the fun of it, I'm writing a program to ...
0
votes
1answer
108 views

Should smaller data types be used in intermediate code?

I often use smaller data types in structs/classes when it is needed in memory savings. I also use them in network/disk IO. My question is if intermediate code that use those objects with smaller data ...
-2
votes
1answer
176 views

Calling an entire script (in C) from another script (in C++)?

I am a very inexperienced programmer, so apologies if this is a stupid question to be asking. And similarly, if anyone answering could assume that I know basically nothing at all and treat me like an ...
1
vote
3answers
195 views

On a modern compiler, how do I intentionally code for 2s complement with wraparound?

I want to compare sequence numbers (given to this code from elsewhere) that may wrap around. Simply comparing two such values will not handle the case like 0x00000002 being greater than 0xfffffffd, ...
0
votes
0answers
23 views

Creating and passing new object in parameter

I have a wrapper class which contains a smart pointer (unique) which can point to objects which are subclasses of State. Each subclass of State receives a reference to the wrapper class so that the ...
4
votes
4answers
403 views

Are there any alternatives to dependency injection for stateless classes?

I am working on an application whereby I have designed classes to fit into several groups: Immutable: initialised through constructors, uses copy-and-swap idiom (inc. move), can deep copy (i.e. ...
1
vote
1answer
111 views

when writing platform specific code, is using separate .cpp always preferred instead of using #ifdef?

I have some platform specific code, such as a string needs different values at different platforms like it: test.cpp #if(PLATFORM==ANDROID) string url="android"; #elif(PLATFORM==IOS) string ...
0
votes
1answer
87 views

What data structure to use to save the address of edges in depth first search?

I am currently trying to write my own Depth First Search. I have created a class called node. class node{ private: bool is_visited; <data structure to collect edges> ...
2
votes
5answers
350 views

Why do I need to declare virtual functions as such?

Example: We have a base class Base and three subclasses which all implement their own version of doSomething(). In an intermediate function f(Base b), we want to call the relevant version of ...
0
votes
2answers
100 views

Best Communication strategy for monitoring dashboard

I have a requirement where I need to display certain stats on Admin Dashboard in my web application (Angular+Java). These stats (transnational data) are being generated on different servers (connected ...
2
votes
3answers
150 views

C++ vs Java: Pointing to Objects

In Java: private State current_state; As "State" is a superclass, I can then assign subclass objects to current_state, effectively making current_state point to a different object: current_state = ...
0
votes
1answer
63 views

How to traverse “linked list” like structure with parallel nodes

I am working on a signal processing project that allows for parallel processing on data streams as well as inputs and outputs to and from multiple endpoints. The signal chain is broken down into "...
4
votes
2answers
104 views

How to facilitate thread-safe access to large set of shared variables?

I have 2 sets--inputs and outputs--of 70 32-bit integer variables and 70 bools (140 vars altogether). These need to be accessed and modified from 3 threads. What is an appropriate design pattern to ...
2
votes
1answer
38 views

Configuration related defines in public header?

Suppose you're building some library, C or C++ doesn't really matter for this question, IMO. The features (or implementation thereof) depend on capabilities of the target system. A simple, probably ...
2
votes
0answers
76 views

How to combine template and inheritance

I have multiple factories, each is responsible for creating instances of an inheritance tree. For example (syntax or semantics maybe incorrect), struct InterfaceA {}; struct ImplA1 : InterfaceA {}; ...
4
votes
2answers
153 views

Passing readonly structs in C++

In this particular problem I am having, I am not sure of the proper way to deal with readonly structs, passed to class constructors as a parameter when I want to store the data in the struct, in the ...
-1
votes
0answers
30 views

Help me understand this flow for recursive function [duplicate]

This recursive function blows my mind a bit because it executes the recursive calls until it hits 1. It returns n, but afterwards it does the multiplication below it four times and returns the final ...
2
votes
2answers
207 views

In C++, why shouldn't all function parameters be references?

I am currently learning C++ from the C++ Primer 5th edition. The book's chapter on functions states that only large objects (large being relative as standard library strings count, but "primitive ...
49
votes
10answers
11k views

Why would the 'final' keyword ever be useful?

It seems Java has had the power to declare classes not-derivable for ages, and now C++ has it too. However, in the light of the Open/Close principle in SOLID, why would that be useful? To me, the ...
1
vote
1answer
108 views

Static memory idiom

I am on a micro controller (which means I can only have static memory allocation) and I am trying to work with inheritance..... Suppose I have a abstract class Image and an abstract class Font. An ...
5
votes
3answers
306 views

Why does ifstream.eof() not return TRUE after reading the last line of a file?

When a beginner starts off reading ifstreams, his/her instinct is to read the file using a loop that usually looks like this: while (!ifstream.eof() { ... } However, C++ programmers note that what ...
2
votes
1answer
128 views

Fine tuning details in C++ function for different customers

The company I work for follows a business model where a core product exists (originally written in C, now in C++) with some default implementation but details are tailor-made for each customer (there ...
-1
votes
1answer
150 views

What is the faster way to save and browse objects using vectors +/or maps? (c++)

I'm doing a object Garbage class using c++: #include "Object.h" class ObjectGarbage { public: ObjectGarbage(); ~ObjectGarbage(); void Call_All_Objects() { //(...
4
votes
2answers
138 views

Calling different library functions based on parameter

I am writing a rather large class where the user can specify at runtime whether she wants to use algorithm A or B of an third party library e. Basically there is only one function call different in ...
17
votes
1answer
461 views

How does Rust diverge from the concurrency facilities of C++?

I am trying to decide whether Rust fundamentally, sufficiently improves upon the concurrency facilities of C++ that I should spend the time to learn Rust. In light of Stackexchange's format, I should ...