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

learn more… | top users | synonyms

2
votes
1answer
103 views

Template Function: Passing Iterators

I am struggling with making a design choice in the following setup: I am writing (C++) functions which take a pair of iterators (to template containers) and compute a return value of the same type ...
-5
votes
0answers
40 views

C++ - how is it with real numbers? [migrated]

int main() { float fNumber = 1234567.89; cout << (fNumber + 1) - fNumber << endl; cout << (fNumber - fNumber) + 1 << endl; } results: 0 1 Can anyone explain ...
1
vote
4answers
303 views

const reference and const pointer. How do they work?

I read a post about how const storage works. How does const storage work? (Item 2, Scott Myers Effective C++) This says that each segment has separate section of write protected memory and const ...
-3
votes
0answers
35 views

How to define member function in separate source file? [migrated]

Consider following two programs. p1.cpp #include <iostream> struct test { void fun(); }; int main() { test t; t.fun(); } p2.cpp #include <iostream> void test::fun() { ...
-2
votes
2answers
141 views

Is it ever overkill creating classes/objects? [duplicate]

I just learned how to create a class or object in C++ and I'm practicing and learning. I'm writing a text based version of a popular board game and I was wondering if it's overkill creating a class ...
23
votes
3answers
4k views

Did C++11 address concerns passing std lib objects between dynamic/shared library boundaries? (ie dlls and so)?

One of my major complaints about C++ is how hard in practice it is to pass std library objects outside of dynamic library (ie dll/so) boundaries. The std library is often header-only. Which is great ...
0
votes
3answers
119 views

A secondary “type system” for references?

I'm designing a language and was wondering how to incorporate C++-like references with regards to their place in the type system. I think they're useful for operations like indexing and dereferencing ...
1
vote
1answer
47 views

Qt/QML/C++ Application database connection object visibility

I am working on some Qt/QML/C++ app and I've got myself into some kind of dead end. Namely, this app uses mysql database and here is my questions: Is performance hurt if I instantate new database ...
4
votes
0answers
61 views

Strategy for OS X and downlevel version of C++11 library?

I think I've found myself in a situation where things are quickly spiraling out of control, and I'd like to get some feedback to select a way to proceed. BACKGROUND I'm working with a library based ...
4
votes
0answers
84 views

What is the correct place to put checking of conditions in this use case?

Here is the situation: I have a database and a class that is basically a representation of a set of fields of different tables in it. Let's call it Message. "Message" does not know of the database ...
3
votes
2answers
132 views

Are string characters assigned a numerical value in context of how they are assigned in a programming language?

For context I am fairly new to programming after returning back to it after programming a number of years ago... I was reading Bjarne's intro programming with C++ and was wondering if alphabetical ...
3
votes
4answers
199 views

Design: Lisp (or other scripting language) as an interactive interface for C++?

I'm working on a medium size C++ project (will probably end up around 50k lines) and I have to provide an interactive terminal interface. The program produces scientific data as an output and the ...
13
votes
10answers
1k views

Does low latency code sometimes have to be “ugly”?

(This is mainly aimed at those who have specific knowledge of low latency systems, to avoid people just answering with unsubstantiated opinions). Do you feel there is a trade-off between writing ...
1
vote
1answer
82 views

How to handle design changes for auto_ptr deprecation in C++11?

We are testing a library under C++11 (i.e., -std=c++11). The library uses auto_ptr and this pattern: Foo* GetFoo() { autoptr<Foo> ptr(new Foo); // Initialize Foo ...
4
votes
1answer
500 views

Is it poor form to use C features such as the size_t type instead of their c++ counterparts, such as std::size_t?

I have recently been told that using size_t as declared in the global namespace is incorrect in C++, ostensibly because size_t is a C-feature. I looked this up and came across this question on Stack ...
1
vote
1answer
157 views

What would be a reason not to embed JS in C? [closed]

I'm working on an opensource application that processes JSON in C, because I would like to make it easier for people to help me in the project and because JSON is so close to JS. I want to embed a JS ...
1
vote
0answers
62 views

Pointers vs IDs for “context-specific” functions

I need to be able to load, use, and free resources from a single monolithic object. I have two requirements: 1. That the resource loaded is owned by the object that created it and 2. All objects in a ...
83
votes
14answers
12k views

Does auto make C++ code harder to understand?

I saw a conference by Herb Sutter where he encourages every C++ programmer to use auto. I had to read C# code some time ago where var was extensively used and the code was very hard to ...
2
votes
2answers
176 views

Is it bad practice to use constant variables with global scope in C++?

So I was reading section 3.2 (One definition rule section) in the latest working draft of the C++ standard and came across this: Every program shall contain exactly one definition of every ...
2
votes
1answer
213 views

How often should RAII be used? [closed]

I've been attempting to learn C++, but it is famously plagued by bad tutorials. I learned about a clever little trick called RAII (Resource Acquisition is Initialization), where one wraps a heap ...
6
votes
5answers
1k views

C++ Iterators: Best practice to represent end of range - Last or Beyond-last?

I am writing a library which deals a lot with sub-sequences of ordered containers. So for example I have a container (1,2,3,4,5,6) and a user wants to access (3,4,5). I am providing the subsequence ...
4
votes
2answers
116 views

C++ design for encoder/decoder classes with different stored types

I need to handle different elements in a vector, each element owning a specific parameter (integer or string), so that I can easily handle encoding/decoding of a series of elements. Encoding a list ...
0
votes
4answers
202 views

How to write procedural code in an object oriented language?

I recently started working in domain of mesh generation . My programs usually contains large chunks of procedural code consisting of several phases. Eg. class MeshAlgo1 { /* A very long function ...
9
votes
4answers
2k views

rand() gives same numbers again for a small range

I'm trying to make sort of a game where I have a a grid of 20x20 and I display a player (P), a target (T) and three enemies (X). All these have an X and a Y coordinate which are assigned using rand(). ...
0
votes
2answers
156 views

Are private members useful anymore? [duplicate]

Watchpoints and data break points make it possible to watch the changes of a value in memory in many languages. Much of the justification I have seen for getters and setters and private variables ...
8
votes
6answers
1k views

Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?

I'm having a bit of a debate with my friend over whether these two practices are merely two sides of the same coin, or whether one is genuinely better. We have a function which takes a parameter, ...
1
vote
0answers
72 views

Data sync local LevelDB store with server? [closed]

I'm building a cross platform application in C++. I need to have local JSON data storage that is syncable to a server. LevelDB seems like it could fit the bill. There is a Node module ...
6
votes
2answers
783 views

Is there a standard way to indicate that a function returns a new pointer?

Sometimes I want to delegate the construction of objects that a class owns to a separate function. Something like Vertex* new_vertex(const Options& options) { // do stuff... return new ...
5
votes
2answers
149 views

How to setup build system for unit testing + mocking?

We have a legacy code base in entirely C++. Our build system is CMake. My first stab at unit testing was as follows: Define a target (LibraryA). This library contains the code to test. Define a unit ...
2
votes
2answers
186 views

Blocking function call with asynchronous content

I am sure that this is a common design pattern, but I seem to have a blind-spot. I have a requirement that the function call from Application to Service be blocking, but service needs to do ...
12
votes
9answers
657 views

Inheritance gone wrong

I have some code where a good inheritance model has gone downhill and I am trying to understand why and how to fix it. Basically, imagine you have a Zoo hierarchy with: class Animal class Parrot : ...
7
votes
2answers
318 views

Const C++ DRY Strategies

For avoiding non-trivial C++ const related duplication, are there cases where const_cast would work but a private const function returning non-const wouldn't? In Scott Meyers' Effective C++ item 3, ...
2
votes
0answers
51 views

Should the order of definitions correspond to the order of declarations in header/source files? [closed]

If my header looks like this: class foo { public: foo(); ~foo(); QString b(); QString c(); QString a(); void setB(QString s); void setC(QString s); void ...
2
votes
2answers
63 views

Reading using non-blocking IO on a fd asynchronously in C++ (moving from Node.js)

I'm trying to switch my brain from Node.js/Objective-C iOS programming to C++ programming, and it's a little bit taxing. Node.js and Objective-C with iOS do not have a run loop that I am supposed to ...
3
votes
1answer
74 views

Design approach to outsourcing a socket in an own thread

I want to extend an existing server written in C++ to be able to communicate with an other server over websockets (e.g. logging or user authorisation purposes). All examples (including the very well ...
2
votes
1answer
135 views

What design pattern best suits managing handles to objects, without passing handles or Manager around?

I'm writing a game in C++ using OpenGL. For those that don't know, with the OpenGL API you make a lot of calls to things like glGenBuffers and glCreateShader etc. These return types of GLuint which ...
0
votes
0answers
69 views

Why are datatypes different in terms of bit size based on complier, and OS used

I was reading a tutorial on C++ as I am new to programming, and I was wondering why the sizeof operator gives different output depending on what you're programming with.
4
votes
2answers
204 views

Rust-style error handling in C++

I've been reading some articles on how Rust does error handling using the Result<T, E> type and to me it seems like a hybrid best-of-both-worlds (exceptions and return codes) solution which can ...
3
votes
1answer
164 views

When writing a math library, will operator overloading maintain OOP?

I want to make a fairly complicated mathematical library for a personal project and then have a visualization/GUI to go along with it to demonstrate the maths - nothing hardcore like topology but just ...
1
vote
1answer
171 views

audio file processing in C++

I thought of the following project that I want to pursue: I want to create a C++ program, that can play audiofiles and visualize the amplitudes of individual frequency bands in real time (e.g. with ...
0
votes
1answer
102 views

Modular programming practices

As I start to code more and more, for example in C++, I tend to also want to learn more about the accepted standards for code. I'm trying to code a chess engine right now, but I'm unsure how to treat ...
3
votes
2answers
119 views

C++ using shared_ptr with API [closed]

I'm building a library that generates a couple of types of objects that can be used by user code and the library. To keep track of these objects, I'd like to use shared_ptr's, so I can build in some ...
4
votes
3answers
185 views

Strategy for fixing signed/unsigned warnings

I'm working on obtaining a clean compile with -Wsign-conversion for existing library code. The library is 25 years old or so. Eventually, this higher bar will be a security gate, and all check-ins ...
2
votes
1answer
54 views

How does condition_variable::notify_one() choose which thread to unblock?

notify_one() of C++ 11 thread library is used to unblock one of the waiting threads. How does it choose which thread to unblock? To begin with, I tried googling but could not find any appropriate ...
0
votes
2answers
149 views

bool accepts int literals, char literals, string literals - why?

Programming: Practices and Principles - Bjarne Stroustrup Drill in Ch 5 - Errors He wants us to correct the code: boo c = "fool"<s; On correcting boo to bool, this is a comparison, so that's ...
3
votes
2answers
120 views

In C++, when should I use final in virtual method declaration?

I know that final keyword is used to prevent virtual method from being overriden by derived classes. However, I can't find any useful example when I should really use final keyword with virtual ...
-1
votes
2answers
182 views

How to debug a program after it appears to fix itself [closed]

While chasing a segfault around a complicated and grouchy c++ program I added several //comments and cout statements, but no 'actual' code. Then, suddenly, for no apparent reason the segfault ...
-1
votes
1answer
64 views

execute selection in console in C++ like python or R language [closed]

I am started to learn C++ using as IDLE Clion of jetbrain company. Normally using the IDLE of Python (i.e., Pycharm of Jetbrain) or R project i am able to execute selection in console line-by-line my ...
2
votes
2answers
214 views

How do I make this functional DSL written in an imperative language more efficient?

Suppose I create a simple functional Domain-specific language (DSL) using an imperative language, in this case C++. Here is a simple implementation of a DSL that can has the notion of a simple value ...
1
vote
1answer
91 views

What's the quickest and most maintainable way to implement SQL querying of disparate C++ variables?

I've recently joined a team working on radar observations. The team has access to a repository of software (written in C++) used for dealing with these observations, but their work is focused towards ...