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

learn more… | top users | synonyms

2
votes
1answer
87 views

Including standard library headers only once across multiple program headers

I am writing a program with multiple headers and code files, as so: message.h message.cpp option.h option.cpp main.cpp message.h includes option.h as message.cpp requires the definition of the ...
2
votes
1answer
209 views

Refactoring code with just a few lines different

I have two for loops in a function which look like these: for (int i=0; i < MAX; ++i) { identical_lines identical_lines identical_lines first_for_specific_line identical_lines ...
0
votes
2answers
1k views

Visual Studio c++ Windows forms? [closed]

I've been slacking off with learning how to program because I'm at my wits end trying to figure out the next step. I want to be able to create forms applications just to test random things and give ...
1
vote
2answers
124 views

Is returning a reference to inside a private vector bad practice?

I have the following trimmed class: #include <vector> #include "Tile.h" class Board { std::vector<Tile> boardArr; // VVV Bad Practice? Safe? Tile& getTileAt(unsigned ...
2
votes
2answers
141 views

Alternative to using getters/setters in a simple class

I'm writing a minesweeper game with curses. To represent each tile, I wrote small Tile class that contains information about the possible state of each tile (whether or not it's flagged, whether or ...
4
votes
3answers
532 views

Intersection of geometric entities

I was trying to design a small C++ geometric API for learning purposes, but came across a problem when dealing with intersections of geometric entities. For example, the intersection of a line and a ...
5
votes
2answers
2k views

Every function is a closure?

Wikipedia says, that closure - is a function, which has an access to variables, declared outside of the function. There is even an example: function startAt(x) function incrementBy(y) ...
4
votes
1answer
234 views

How does modulus work?

I am not asking asking how the mathematical concept of modulus works, or how 20 % 3 == 2 I understand this fine. I'm more curious how the compiler/interpreter determines this. I can think of a very ...
-1
votes
0answers
21 views

Using Command Line Arguments to Choose a Datatype? [migrated]

Basically, based on whether a user inputs a 0 or 1 as a command line argument, I want my data class to either have a vector or an array (static) as its data member. I don't believe you can add if ...
0
votes
2answers
138 views

Style question: To use overloaded version or not?

Let's say I had an operator() overload that made temp_.matrix[k][j] and temp_(k+1, j+1) equivalent. In my class functions (where matrix belongs), would it be better to have if (temp_.matrix[k][j] == ...
14
votes
7answers
3k views

Are header files actually good? [closed]

I find header files to be useful when browsing C++ source files, because they give a "summary" of all the functions and data members in a class. Why do so many other languages (like Ruby, Python, ...
4
votes
4answers
256 views

What makes a program cross-platform or not?

I have fairly basic programming experience with Java and I've tried C++ and Python. While it makes sense for Java, the basic programs I've written in C++ have run just fine on Windows and OS X. I've ...
-1
votes
0answers
27 views

how to create a char array in c++? [migrated]

I am trying to create a pointer to a char and pass it to function as a char*. here is my code to define the char array it seems the compiler does not recognize the end of the string because the ...
-2
votes
0answers
25 views

Addition and Subtraction giving incorrect results [migrated]

I made a simple program to add and subtract the given numbers. Let's say the 2 numbers give were 5 and 5. It would print 5 + 5 = 10 and 5 - 5 = 0. Right now I'm not sure what's wrong. I might need a ...
0
votes
0answers
40 views

Difficulty designing my EventHandler class that uses a callback to handle key-presses

I wrote an EventHandler class that takes a std::function<void(char)>, and a std::vector<char> of chars (representing keys). If a key in the vector is pressed, the call-back is executed ...
-2
votes
0answers
59 views

The correct way to use a for loop in an array C++ [migrated]

I'm having some trouble with a homework assignment dealing with Arrays. I have already read all the text from class (which didn't help me at all), looked up several tutorials on arrays, and searched ...
4
votes
4answers
2k views

C++ Iterator lifetime and detecting invalidation

Based on what's considered idiomatic in C++11: should an iterator into a custom container survive the container itself being destroyed? should it be possible to detect when an iterator becomes ...
2
votes
3answers
343 views

How to manage focus for a small set of simple widgets

I'm developing a set of simple widgets for a small (128x128) display. For example I'd like to have a main screen with an overlay menu which I can use to toggle visibilty of main screen elements. Each ...
-2
votes
0answers
16 views

Using getline() with parameters to read through a file and extract specific information? [migrated]

I'm programming a c++ console application for school and I need to read through a .txt file to extract specific informations to use them in classes. For example, here is a portion of the txt file : ...
30
votes
8answers
2k views

The “blub paradox” and c++

I was reading the article here: http://www.paulgraham.com/avg.html and the part about the "blub paradox" was particularly interesting. As someone who mainly codes in c++ but has exposure to other ...
0
votes
3answers
197 views

Compile-time vs. run-time configuration

I'm starting new project and need to decide on how to handle configuration. I recently run into suckless project. While it has its own issues, I really liked its approach to configuration - just a ...
164
votes
9answers
11k views

Why do many exception messages not contain useful details?

It seems there is a certain amount of agreement that exception messages should contain useful details. Why is it that many common exceptions from system components do not contain useful details? A ...
5
votes
2answers
3k views

Refactoring C++ [closed]

I know that there are some other questions about C++ refactoring, but none satisfies my needs. I have a background in Java and Python programming, but now I'm approaching C++. I've read (and ...
11
votes
6answers
2k views

Homework for strong C++ developers learning Java [closed]

I'm tasked with teaching an accelerated university special topics course in Java for programmers with many years of experience in C/C++. What sort of medium-length (1-2 hours max) assignments would ...
11
votes
5answers
1k views

How to migrate my thinking from C++ to C#

I am an experienced C++ developer, I know the language in great details and have used some of its specific features intensively. Also, I know principles of OOD and design patterns. I am now learning ...
6
votes
2answers
209 views

Static console object or dependency injection?

For a current project I have created a Console class which wraps up C++'s usual cout, writes output to a file or uses Console::WriteLine depending on the environment. Currently I've setup my code base ...
2
votes
3answers
357 views

In C++, should I ever declare a local variable in the corresponding header?

So for example, I create a class with two functions: foo.h class foo { public: foo(); void bar(); void ey(); bool m_memberBool; bool localBool; // ??? Should I put this here? }; ...
6
votes
2answers
516 views

Deploying Qt-based app on Mac OS X

I wrote a cross platform application and I distributed it successfully for Windows and Linux (and tested them on "clean" virtual systems, with no other additional software and/or libraries installed). ...
1
vote
1answer
141 views

Are there real world examples demonstrating reasonable performance improvement by using move semantics?

(I've asked a similar question on SO but unfortunately it might not be proper, so I also put here; please kindly point out if you think it's a duplicate.) I've heard many words about the move ...
-1
votes
2answers
173 views

Mermaid problem (OOP) [closed]

A Mermaid is half fish and half human. It has upper human body and lower fish body. When under water its upper part swims like a human while its lower part swims like a fish. Please explain how can it ...
1
vote
4answers
692 views

Should non-interface code be hidden from the client?

I am working on a library which had several headers that are meant to only be used by the library itself. I also have a few classes and functions in headers that I do not want the client to use. For ...
32
votes
6answers
13k views

Why is there no 'finally' construct in C++?

Exception handling in C++ is limited to try/throw/catch. Unlike Object Pascal, Java, C# and Python, even in C++ 11, the finally construct has not been implemented. I have seen an awful lot of C++ ...
-1
votes
1answer
50 views

One symbol - one meaning?

It's well known recomendations that: 1) class or function should have one responsibility 2) variable should not be reused in different contexts We can generalize this recomendations to general ...
0
votes
1answer
145 views

Estimed number of tries

Problem: The Oscar Committee wants to decide which person should get the best actor award among the given N actors.For that they decided to use a random function random_bit() which returns either 0 ...
-1
votes
1answer
204 views

Optimal movie schedule

Let's say Mr. A goes to cinema and he gets a movie schedule. There are N movies on the program today (1 < N < 50) and for every movie the time of the start and the time of the end are given in ...
7
votes
3answers
774 views

Apple eating problem

Player A and Player B play a game. On the middle of the table there is a pot full of N apples of different weights. Player A starts first and choose an apple and start eating it. Losing no time player ...
2
votes
1answer
1k views

How is C++ in QT different from the standard C++ libraries?

I want my app to be able to manipulate images and create watermarks and also needs to run on both windows and mac. Hence I've been investigating QT. Does QT ship with all the libraries I will need to ...
1
vote
0answers
81 views

Is the behaviour of floating-point arithmetic defined by the c++ standard?

I started writing this as a SO question but I think it's better suited here. While I'm happy for simply an answer I'm also fond of the "Teach a Man to Fish" phylosophy so I'm happy for people to point ...
0
votes
1answer
462 views

Reading a character from a file stream in c++ (AKA: Why no C++ standard equivalent of old conio.h)?

This is a question of about design decisions. I find that there are so many different things I work on where reading a _kbhit() is amazingly useful but I can't find (read: there isn't) any ...
2
votes
0answers
63 views

Can't figure out class scheduling issue? [closed]

"You are a college advisor trying to help students plan their academic careers. You have the list of courses in each major and their prerequisites. Students often want to take their major courses as ...
12
votes
4answers
10k views

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the ...
16
votes
8answers
1k views

How to spread awareness for generic programming among team members?

I am staying in an environment, where people believe: Java generics are the feature exclusively used for library writing and not for the real coding. C++ is an OO programming language; template is ...
33
votes
17answers
7k views

Teaching C++ to first time high school students: Where to draw the line?

I will be mentoring a team of high school students for the FIRST Robotics Competition, most teams here develop[ their robot software using C++. For many of the students on the team this will be their ...
3
votes
2answers
256 views

How to iterate through all permutations of valid links between nodes

Imagine a 2D area, with a number (array) of nodes (or points) defined within it, in arbitrary (but known) positions (integer x,y coordinates), like this: From there I want to be able to, ...
0
votes
1answer
85 views

Receiving information from another application [closed]

I am currently attempting to create an AI which can play games, but learns to play rather than using hard-coded instructions. But, this isn't my problem, my problem is telling the AI whether it has ...
1
vote
1answer
80 views

Run a c++ program under lots of different data maps

I want to run a c++ program to process a lot of data from different xml files and output results. I run the program once per file and potentially have around 50 different files. The trouble is each ...
3
votes
2answers
281 views

Transforming XML to JSON in C++ [closed]

Working with XML in C++ seems a bit of a pain and I'm looking at a way to output JSON. I've stumbled on two different approaches: A) XSLT transformations: ...
0
votes
1answer
119 views

Moving from C++ to NodeJS - Working with XML and JSON

I need to make a program to compare several large +100MB XML files, find the difference, and spit out JSON. I have written the comparison code in C++ and it performs great but now when I am getting to ...
2
votes
1answer
92 views

How minor of a case is a class appropriate for?

I'm trying to use composition in an Entity class of mine. So far, an Entity "is" Displayable(inherited an ABC), and "has" a Position(contains a point-like class). I know I will want my Entity to ...
4
votes
2answers
168 views

Is there any harm in having classes made up mostly of inherited classes?

Say I have a few base classes: HasPosition supplies a 2D location, and methods to "move". IsDisplayable defines how a class will be displayed in a given graphics library (say, curses), and methods ...