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

learn more… | top users | synonyms

33
votes
9answers
7k views

Is the algorithm more important than the programming language?

During the current (2013) Google Code Jam contest, there was a problem that took C++ and Java people 200+ lines of code as compared to Python people that solved the same problem only using 40 lines of ...
1
vote
3answers
141 views

Do methods Merge and Join make sense for a path class?

I'm writing a path class which currently has a method called Combine which works like the .NET Path.Combine. If an argument is an absolute path (roughly because it begins with \), then it replaces ...
10
votes
1answer
794 views

Object lifetime invariants vs. move semantics

When I learned C++ long ago, it was strongly emphasized to me that part of the point of C++ is that just like loops have "loop-invariants", classes also have invariants associated to the lifetime of ...
0
votes
0answers
114 views

What to unit-test when loading a dll?

I wrote a wrapper class that is using a dll. I have a LoadLibrary() function and have wrapped all the functions the dll exports (to match my error codes). I am not too familiar with unit testing. I ...
7
votes
2answers
343 views

Compile-time checking for NULL initialized std::string

This is sort of the complementary question to How to best protect from 0 passed to std::string parameters?. Basically, I'm trying to figure out whether there is a way to have the compiler warn me if a ...
5
votes
2answers
197 views

C++ class design with invariant

I've been pondering a really basic question about how far to take enforcing a class's invariant. Maybe that's worded badly, so as an example, let's say that I want to write a class which stores a ...
12
votes
11answers
17k views

Avoid Postfix Increment Operator

I've read that I should avoid the postfix increment operator because of performance reasons (in certain cases). But doesn't this affect code readability? In my opinion: for(int i = 0; i < 42; i++)...
1
vote
1answer
174 views

Function pointers vs. Derived classes

So I have a Signal class and a client class which manages a list of Signal objects. The class provides a bunch of interfaces, one of them is an update()-function. The update()-code is completely ...
5
votes
1answer
170 views

Importing namespaces inside another namespace

I generally like to organize classes I make into modules by using namespaces, and I also don't go more than 2 namespaces deep but it's still painstakingly hard to fully qualify everything. I've ...
2
votes
0answers
56 views

Visual interpolations: independent or linked to object?

I have a class that I use to render GUI elements on the screen, this class has a tree structure (with children, parent, and siblings). I created an additional class which allows me to interpolate some ...
4
votes
1answer
132 views

Which C++ IO interfaces for a complex data source i.e. converter

My question: When you've got a complex converter like, that takes chunks or large result sets out of a database, converts it into a line by line file/resource in the end, should one either design it ...
25
votes
7answers
2k views

Should a C++ program catch all exceptions and prevent exceptions from bubbling up past main()?

I was once advised that a C++ program should ultimately catch all exceptions. The reasoning given at the time was essentially that programs which allow exceptions to bubble up outside of main() enter ...
12
votes
2answers
2k views

Is using Exceptions at the highest level of a program considered bad practice? [duplicate]

I have seen programs using this strategy and I have also seen posts considering this bad practice. However, the posts considering this bad practice have been written in c# or some other programming ...
0
votes
1answer
170 views

How does the base class non-virtual function get called when derived class object is assigned to base class?

#include <iostream> class Base { private: int b_value; public: void my_func() {std::cout << "This is Base's non-virutal my_func()" << std::endl; } virtual void ...
1
vote
1answer
67 views

Managing Opengl projects under Team Foundation Server

I am learning OpenGL/C++ by following a tutorial series on in2gpu. I set up my Project just like the autor did. It is an Empty Visual C++ Project. Inside the project I linked glew and freeglut, the ...
3
votes
1answer
1k 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 ...
22
votes
8answers
2k views

How did you become a const correctness convert? [closed]

After 15 years of C++, I've still haven't learn to love using const. I understand it's use, but I've never actually been in situation where being const correct would have avoided the problem I was ...
1
vote
2answers
261 views

Preferable design of scope guard in C++

Recently, I come across an issue about the design of scope guard. A scope guard invokes a supplied function object (usually performs cleanup procedures) upon exiting the enclosing scope. The current ...
105
votes
11answers
34k views

What backs up the claim that C++ can be faster than a JVM or CLR with JIT? [closed]

A reoccurring theme on SE I've noticed in many questions is the ongoing argument that C++ is faster and/or more efficient than higher level languages like Java. The counter-argument is that modern JVM ...
45
votes
5answers
10k views

Why are reference-counting smart pointers so popular?

As I can see, smart pointers are used extensively in many real-world C++ projects. Though some kind of smart pointers are obviously beneficial to support RAII and ownership transfers, there is also a ...
2
votes
4answers
936 views

Common header file for C++ and JavaScipt

I have an app that runs a C++ server backend and Javascript on the client. I would like to define certain strings once only, for both pieces of code. For example, I might have a CSS class "row-hover"...
-1
votes
1answer
75 views

2D Animating in OpenGL [closed]

I am trying to animate a texture in OpenGL. I feel like it should be easy, as I know how to animate in SDL and other libraries but I am having trouble. I have a x,y,w,h that holds the texture ...
12
votes
2answers
4k views

How does C++ handle multiple inheritance with a shared common ancestor?

I'm not a C++ guy, but I'm forced to think about this. Why is multiple inheritance possible in C++, but not in C#? (I know of the diamond problem, but that's not what I'm asking here). How does C++ ...
24
votes
9answers
4k views

What is different between the internal design of Java and C++ that lets C++ have multiple inheritance? [duplicate]

It's drilled into the newbie Java programmers that Java (pre-Java 8) has no multiple class inheritance, and only multiple interface inheritance, because otherwise you run into diamond inheritance ...
2
votes
3answers
181 views

Why does the overriding rule of C++ not care about visibility changes?

In C++, it is possible to write an overriding for a base class's method even if the visibility declaration of the two don't match. What are the possible design considerations under the decision of not ...
2
votes
2answers
3k views

Is there any advantage to c-style bit manipulation over std::bitset?

I work almost exclusively in c++11/14, and usually cringe when I see code like this: std::int64_t mArray; mArray |= someMask << 1; This is just an example; I'm talking about bit-wise ...
3
votes
2answers
277 views

Is it possible to have pointers recursively point into themselves?

I have been learning C++ recently, and upon reading up on pointers I had a moment of thought. I'm still attempting to grasp the very idea of pointers so excuse me if this doesn't make sense beyond ...
0
votes
2answers
198 views

node.js C++ addons to do all major computations [closed]

A major limitation of node.js is its single threaded execution and the fact that JS is slow with computations. What are the advantages/drawbacks of using C++ to do the application's heavy lifting ...
2
votes
0answers
218 views

Is it a good practice to use virtual inheritance as default? [closed]

Why virtual inheritance is not used as default? Default as in by the programmer and not the language. If not, why? What are the cases where it may fail? Is there some run time overhead? Is it ...
0
votes
0answers
44 views

Is it possible to maintain one code base and deploy on both spir/open-cl and ptx/cuda?

It is my understanding that both spir and ptx are based on llvm. Binaries can be generated by clang for use along with the native graphics compute drivers (e.g. open-cl and cuda). My question is ...
15
votes
7answers
40k views

Why are constructors not inherited?

I am confused as to what the problems could be if a constructor was inherited from a base class. Cpp Primer Plus says, Constructors are different from other class methods in that they create ...
2
votes
2answers
696 views

Any OOP design pattern that is somewhat representative of all of the SOLID OOP design principles at play?

I'm trying to teach an object oriented design principles course (on SOLID) at a training institute. I also want to teach the students a few OOP design patterns such as factory, singleton and one other....
1
vote
2answers
151 views

Parameterization vs subclassing

Example taken from : Agile software development : principles, patterns and practices A new employee is added by the receipt of an AddEmp transaction. This transaction contains the employee's name, ...
1
vote
1answer
69 views

Modeling specific objects interaction

I want to model an interaction between classes, e.g. there is a general class Hero and he can have some items. (I came up with this analogy so it is easier to understand) Some of them are e.g. potions ...
3
votes
3answers
244 views

I want to overload a function with the same type parameter; what should I do?

I'm creating an API, and I want to overload a function for strip: QString MyClass::strip(); QString MyClass::strip(QRegularExpression open); QString MyClass::strip(QRegularExpression close); QString ...
11
votes
8answers
538 views

How does a variable introduce state?

I was reading the "C++ Coding Standards" and this line was there: Variables introduce state, and you should have to deal with as little state as possible, with lifetimes as short as possible. ...
2
votes
2answers
424 views

Is it good practice to have your C++/Qt functions always check all its arguments for null values?

Backstory While developing with Qt Signal/Slots, I came across a few segmentation faults that had me puzzled as to what was causing it. Eventually I figured out that you could actually pass a slot ...
23
votes
1answer
2k views

Why does the documentation on some languages say “equivalent to” rather than “is”?

Why does the documentation on some languages say "equivalent to" rather than "is"? For example, the Python Docs say itertools.chain(*iterables) ... Equivalent to: def chain(*iterables):...
7
votes
5answers
387 views

What does it mean to declare a volatile variable?

Many low level programs use the volatile keyword for types for memory mapping and such, however I'm sort of confused as to what it REALLY does in the background. In other words, what does it mean ...
5
votes
4answers
466 views

Trying to get a better understanding of object-oriented design's performance pitfalls

I'm currently in a high-performance computing class taught in C++. Usually, I do work in C#, meaning everything is taken care of for me, and optimization comes behind maintainability and higher-level ...
8
votes
3answers
387 views

Anonymous namespaces make code untestable

Here is a typical C++ code: foo.hpp #pragma once class Foo { public: void f(); void g(); ... }; foo.cpp #include "foo.hpp" namespace { const int kUpperX = 111; const int ...
3
votes
6answers
234 views

OpenGL, multithreading, and throwing destructors

How do you make an class that properly warns a developer in the future that they've made a mistake somewhere in their implementation that resulted in an object that gets deconstructed in a state that ...
2
votes
3answers
2k views

Design choices when doing multithreading in C++

OK, so, background: I have a little "server" app/module that is written in C++. This is kindof a tunnel application that provides a standardized API via TCP/IP[a] and on the other hand talks to the "...
2
votes
3answers
260 views

C++ Virtual destructors used only when there are virtual functions

This is from Effective C++ (Meyers): Classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors I don't understand why non-...
30
votes
9answers
2k views

Is possible to write too many asserts?

I am a big fan of writing assert checks in C++ code as a way to catch cases during development that cannot possibly happen but do happen because of logic bugs in my program. This is a good practice in ...
9
votes
2answers
480 views

Interface Segregation Principle: What to do if interfaces have significant overlap?

From Agile Software Development, Principles, Patterns, and Practices: Pearson New International Edition: Sometimes, the methods invoked by different groups of clients will overlap. If the overlap ...
56
votes
6answers
25k views

Is `catch(…) { throw; }` a bad practice?

While I agree that catching ... without rethrowing is indeed wrong, I however believe that using constructs like this: try { // Stuff } catch (...) { // Some cleanup throw; } Is acceptable in ...
-1
votes
2answers
111 views

Translate MATLAB expressions containing “^” power operator to C++ syntax [closed]

Brief: I have several very large math expressions written in Matlab/Octave syntax. I want to evaluate them within a C++ function by copy and pasting them in, however the problem is they contain "^" ...
0
votes
2answers
309 views

Is declarative programming overrated? [closed]

I've been programming for years with primarily-imperative languages (C++, C#, javascript, python), but have recently experimented with some functional langauges (Lisp, Haskell) and was excited to try ...
2
votes
1answer
83 views

How are objects stored in unordered_sets?

I've been doing a bit of research on the subject. I know unordered_sets are hash tables, where the key and value are one and the same. What I'd like to know is how the compiler figures out where in ...