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

learn more… | top users | synonyms

0
votes
2answers
235 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 ...
3
votes
2answers
282 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
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 ...
2
votes
0answers
224 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 ...
1
vote
1answer
70 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 ...
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):...
1
vote
2answers
152 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, ...
3
votes
3answers
247 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 ...
8
votes
3answers
427 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 ...
2
votes
3answers
269 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-...
2
votes
2answers
448 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 ...
7
votes
5answers
407 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 ...
9
votes
2answers
488 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 ...
-1
votes
2answers
117 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
355 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 ...
0
votes
1answer
70 views

Containers and shared ownership within a class instance

I have two objects as member variables of a class. std::unique_ptr<Object> specificObject; std::vector<std::unique_ptr<Object>> objects; I know that specificObject will always be ...
3
votes
6answers
249 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 ...
4
votes
4answers
444 views

Is it true that calling _exit() instead of exit() won't prevent static destructors from being called?

On comp.lang.c++.moderated@googlegroups.com, Greg Herlihy posted the following extern "C" function: extern "C" { int func() { wchar_t memoryName[256]; wchar_t mutexName[256]; ...
7
votes
4answers
2k views

Should I be using advanced techniques most of the time at my new job just because I can? [closed]

Simply put I'm new to the company, should I rather write advanced techniques with things like templates, std techniques..etc to make a first good impression and have my colleagues trust/be impressed ...
5
votes
4answers
918 views

Does making a method static save memory on a class you'll have many instances of?

In response to Aaronaught's response to the question at: Can't I just use all static methods? Isn't less memory used for a static method? I am under the impression that each object instance carries ...
2
votes
2answers
116 views

Managing widgets in a simple GUI framework

So, I'm still working on my small GUI "library". (C++ with boost) The goal is to provide a simple solution for small SPI displays, using a Raspberry or some embedded board. Thus, I ended up having a ...
3
votes
2answers
111 views

How can I create extensible reader and writer wrappers around a shared opaque resource class?

In C++, Say I have a Buffer class (cannot be inherited) which is given and can support the following operation: class Buffer { public: void write_buffer(Data); Data read_next_data(); }; Now ...
14
votes
3answers
494 views

How wrong is it to talk about C++ “methods” (versus “member functions”)?

I understand that according to the C++ spec there is no such thing as a "method", and some (many? most?) C++ programmers consider "method" to be a Java-ism. On the other hand, even on a C++ forum ...
34
votes
9answers
3k views

Doesn't “always initialize variables” lead to important bugs being hidden?

The C++ Core Guidelines have the rule ES.20: Always initialize an object. Avoid used-before-set errors and their associated undefined behavior. Avoid problems with comprehension of complex ...
7
votes
2answers
2k views

Should one derive / inherit from std::exception?

While designing my first 'serious' C++ library, I'm asking myself: Is it good style to derive ones exceptions from std::exception and it's offsprings?! Even after reading Designing exception ...
25
votes
2answers
3k views

Are header-only libraries more efficient?

Assumptions One of the advantages of header-only libraries for C++ is that they do not need to be compiled separately. In C and C++ inline makes sense only if the function is defined in a header ...
4
votes
3answers
547 views

Avoiding constructors with many arguments

So I have a factory which creates objects of different classes. The possible classes are all derived from an abstract ancestor. The factory has a configuration file (JSON syntax) and decides which ...
5
votes
2answers
213 views

Factories, vectors and smart pointers - Design Question

So, my Business Code needs some Objects. It does not know how much objects it needs and it does not know the exact types (because polymorphism is involved). For me, that sounds for a good reason to go ...
10
votes
4answers
380 views

Parameter management in OOP application

I'm writing a medium size OOP application in C++ as a way to practice OOP principles. I have several classes in my project, and some of them need to access run-time configuration parameters. These ...
25
votes
10answers
5k views

Isn't the use of pointer variables a memory overhead?

In languages like C and C++, while using pointers to variables we need one more memory location to store that address. So isn't this a memory overhead? How is this compensated? Are pointers used in ...
6
votes
2answers
165 views

Does Time Complexity analysis factor for cache performance of an algorithm?

If I have an algorithm A. and it has fewer instructions than algorithm B. but performs worse on a CPU due to poor memory coalescing (and hence, poor CPU cache performance), does that factor into the ...
2
votes
2answers
151 views

Acceptable design to use public member objects in composition?

Say I have a large class called Root, which has a lot of members & functions: class Root { public: void func1(); // operates on a and b void func2(); ... private: A a; ...
1
vote
2answers
282 views

Should I use a jump table or a switch case statement for similar functions but may have some parameters that are not relevant [closed]

I had a HUGE switch/case statement, which I converted to use a function dispatch table using an enum list for the index. My colleague (who is doing a code review on this change) agreed that it should ...
2
votes
1answer
639 views

Number of parallel lines in a given set of lines [closed]

I am given N lines, i.e, I am given a,b,c for N lines. And each line is of the form ax + by + c = 0. I need to find out the maximum number of lines that are parallel to each other. For this, first I ...
11
votes
5answers
632 views

What is the conceptual difference between finally and a destructor?

First, I am well aware of Why is there no 'finally' construct in C++? but a lengthy-growing comment discussion on another question seems to warrant a separate question. Apart from the issue ...
1
vote
1answer
240 views

Refusing to define 2 or more classes in one .cpp file [closed]

In my company there is implicit rule not to define multiple classes inside one .cpp file. The argumentation is that it looks like a mess and hard to read. But in my opinion it will make sense if the ...
0
votes
1answer
206 views

Implement Stack for Toy VM C++

Recently for fun I have decided to build a toy programming, compiler and a low-level register based interpreted vm. While starting to implement the virtual machine I got stuck. The stack which holds ...
-2
votes
1answer
794 views

Bresenham Vs Midpoint Circle drawing algorithm

Can somebody explain the difference between Bresenham and Midpoint Circle drawing algorithms? All I am able to understand is that in both algorithms we compute coordinates of an octant instead of ...
8
votes
2answers
414 views

Merits of copy-on-write semantics

I am wondering what possible merits does copy-on-write have? Naturally, I don't expect personal opinions, but real-world practical scenarios where it can be technically and practically beneficial in a ...
1
vote
0answers
97 views

Running simulations in parallel, managing each separately [closed]

Let me preface this by admitting that I am a novice - this is the first real project I am doing outside of a class. I am running a single-threaded C++ simulation several hundred times (i.e. I have ...
8
votes
2answers
446 views

Is the strong exception safety guarantee with a pass-by-value argument which can throw on destruction possible?

Suppose you have a type with a throwing destructor, and a function receiving it by value. Can that operation ever provide anything better than the basic exception guarantee? Or formulated differently, ...
2
votes
2answers
189 views

Private variables and the old FORTRAN common blocks [closed]

This is a question that perplexes me about object oriented programming. In some OOP languages (e.g. C++) a member function can access private variables of the class without restriction. That means ...
4
votes
2answers
435 views

What alternatives to a singleton are there for a class which only can have one instance?

I need to represent an abstraction over various parts of the hardware for a game. I'm trying to decouple the code that does things like manage the logic of the game from the code that is API/platform ...
4
votes
4answers
1k views

Is measuring the binary size of a c++ program a good indication of code complexity? Or something else?

In my company we have this obligatory practice before a review to be send the binary size impact of the code change to be measured and provided. We must use -Os for this metric to avoid ...
0
votes
0answers
53 views

Do I have to put copyright info in every file of my project? [duplicate]

Do I have to put a copyright phrase on every file that is used in my visual studio projects? does it even make any sense? example: file.cpp // Copyright Company Name 2000-2015 code code code ...
5
votes
2answers
250 views

How to optimize/parallelize the following clustering/joining algorithm:

I have relatively-small algorithm that takes up ~60% of the total run-time of my scientific code (57 lines of 3600), so I would like to find a way to optimize what I'm doing and make the code order-...
1
vote
0answers
72 views

Function behavior dependent on the calling class [closed]

I have a base class B and several children classes of it. Every children B1, B2, B3... has a method that calls a method M of another class C. Class C has a component D (implementing an interface, ...
1
vote
0answers
177 views

What is the “async desaster” Detlef Vollmann was talking about at Meeting C++ 2014?

I found the slides of his talk there: https://meetingcpp.com/tl_files/2014/talks/executors-meetingcpp.pdf In one of the last pages, he is talking about the "async desaster" (which I, as a non-native ...
0
votes
1answer
236 views

Merging Two Binary Search Trees

What time in terms of Big O will it take to merge two BST's in One? Each having no of nodes n and height O(log n) with no common element. Resultant should be also a BST