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

learn more… | top users | synonyms

2
votes
2answers
164 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
396 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
50 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
245 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 ...
1
vote
0answers
68 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
169 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
208 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
3
votes
2answers
197 views

Should I add a layer to keep object always valid and immutable?

I am developing COM. A factory need to be initialized first by providing a ID. I can not pass arguments to IFactory instance via CoCreateInstance. So IFactory may need add a init method to be called ...
12
votes
6answers
1k views

In general, is it worth using virtual functions to avoid branching?

There seems to be rough equivalents of instructions to equate to the cost of a branch miss virtual functions have a similar tradeoff: instruction vs. data cache miss optimization barrier If ...
1
vote
1answer
147 views

undirected c++ graph with a direction [closed]

I want to create a Graph in C++ that has directions, with directions I don't mean the direction of an edge but what I want is an undirected graph where the edges have a direction to the next vertex, ...
2
votes
1answer
289 views

Model-View-Controller design question (One vs. multiple models for multiple view+controller)

I have an issue with shared code in a MVC application. Application controllers/views ABC are different classes but use a common framework. It is a single class reused multiple times. Let me introduce ...
6
votes
2answers
295 views

Automatic namespace resolution in C++

My question is focused on design, and the code included in this question is meant to clarify the problem I'm experiencing. I'm interested in conceptual level answers and not code in order to answer ...
0
votes
1answer
199 views

Where is the dynamic memory associated with std::bind?

The problem is that since you can use std:bind in a loop with an arbitrary number of iterations, the storage cannot be inline. It doesn't appear to be on the heap, since it doesn't appear to generate ...
1
vote
1answer
144 views

State Machine Memory Management Problems

My state machine handles requests and returns the next state. In the below simplification, I've got two states here, CreatedState and RunningState. Running state is the end state, and just returns ...
0
votes
1answer
492 views

How to store various sized values in a vector?

I want to separate the communication protocol from the communication medium in a project. For example, I want to have a serial class and whatever other medium class and separately have a checksum ...
4
votes
3answers
265 views

C++ returning persistent objects

I'm currently trying to learn best practices in C++ after coming from a C# background. I understand that there are three ways of handling objects: By value (objects are copied or moved when passed ...
2
votes
3answers
257 views

Is there any merit for using beginning iterator instead of reference to a std::vector?

I'm working on our company's lib. I see a lot of code like: std::vector<int>::iterator it = market.vec.begin(); for (size_t i = 0; i < market.vec.size(); ++i) it[i] = i + 1; I think a ...
2
votes
1answer
136 views

C++ HTTP API Design

I have a massive library written in C++ that handles a lot of mathematical operations, using C++ makes it fast and in all honesty, I don't want to re-write the whole functionality. I am thinking ...
0
votes
1answer
118 views

Function or class design in library API for efficient object reuse

I am currently building a library in C++11, where I spent a lot of time trying to design a good interface. After some small redesigns along the way, I've ended up with a design that I am happy with. ...
-3
votes
3answers
93 views

Link to ISO doc? Complexity of unordered_map operations in C++::stl [closed]

In different forums, I have found the statement that in C++::stl, insert and find for unordered_map have guaranteed amortized time complexity O(1). This puzzles me, and I would like to see the ...
1
vote
2answers
95 views

Fully Specifying #includes

I'm very fastidious about code. When I remove functionality from a file I remove the cooresponding #includes. I've hit several errors where I would remove a #include from a header, and another file ...
0
votes
0answers
88 views

How to improve the design pattern of this Screen Window system?

I have a Screen Window system, a Screen (IScreen as interface), which holds a window, either WindowA or WindowB. System can switch window by calling stateChanged: class IScreen{ public: virtual ...
0
votes
1answer
224 views

C++, header files and using namespace std

So I understand that one must not use using namespace std; in header files. Well for my assignment we have to overload the >> operator. I got my program to run and all, but I had to include the ...
2
votes
1answer
256 views

Feeding std::streambuf for Non-Blocking std::istream Read

I want to write small library to guide a child process for a specific needs (not trying to reinvent bicycle here). My needs includes reading from child stdout and stderr. I want to implement this as ...
2
votes
3answers
185 views

Call by value for classes c++ design decision [closed]

I wonder why C++ uses 'call by value' for classes as default? I imagine this could have advantages in the sense of performance (speed). I mean C# and Java pass classes as references as default. What ...
4
votes
3answers
309 views

What are R-value references used for?

I'm trying to dig deep into C++ and really learn the nuances of the language but one thing I've found to be really confusing is the R-Value reference. The whole double ampersand thing to be clear (in ...
3
votes
0answers
149 views

Is it okay to write C code that must be compiled with -fno-strict-aliasing? [closed]

Some major C projects violate the strict aliasing rules in the C standard and must be compiled with -fno-strict-aliasing. These include Python, OCaml and the Linux kernel. Obviously this is not ...
1
vote
1answer
166 views

Data structure to use for time-optimized line-vs-grid check?

I am asking here because I feel that this question can be filed under the "algorithm and data structure concepts" category. Background: I have been recently assigned the task to design and develop a ...
4
votes
1answer
191 views

Optimal fixed-size sequential sorting algorithms

I have been working on sorting algorithms for a few weeks now, but one of my question still hasn't an answer: are there optimal sequential comparison sorts for fixed-size – and random-access – ...
5
votes
3answers
366 views

non-optional pointers vs. non-const references in C++

In Other C++ Features, Reference Arguments of the Google C++ Style Guide, I read that non-const references must not be used. All parameters passed by reference must be labeled const. It is clear ...
-6
votes
1answer
222 views

Using delete this to free memory in class

Whenever we are writing destructors in C++ (though this concept could apply in other languages) could we just not write delete this; and it would delete whatever data is inside of the class? Is ...
2
votes
1answer
94 views

How to generate learning material from your work [closed]

Catalyst: Recently my team has been tasked with bringing in house a large software project that has been worked on for over 20 years. Some of the old development team is still around, and originally ...
5
votes
8answers
306 views

Designing around shallow constness with inheritance

Background I'm writing an image handling class. For this question two requirements of the class are of interest: Must have "deep" const correctness. Must allow sub-image aliases, a.k.a. sections or ...
133
votes
11answers
20k views

Should I stop using the term C/C++?

I understand C and C++ are different languages but when I was learning C++ I was always told that C is a subset of C++ or C++ is C with classes. And that was quite true until the appearance of C++x0, ...
3
votes
3answers
320 views

C++ Extensible namespaces - how to force declarations back into global namespace

It is good programming style to include all necessary dependencies in a header that references them. Often this includes declarations that are placed in the STD & global namespaces (like cstdio). ...
1
vote
1answer
62 views

Threads Inside Full Duplex Device Library

This is the second project I'm working on which will use a design that I'm not 100% confident about. I'd like some feedback, maybe recommendations for a better design, or verification that this would ...
4
votes
2answers
162 views

Does returning pointer to composed objects violate encapsulation

When I want to create an object which aggregates other objects, I find myself wanting to give access to the internal objects instead of revealing the interface to the internal objects with passthrough ...
1
vote
1answer
97 views

Why some compilers do not synthesize the move operations at all per this paragraph in C++ Primer 5th?

In particular, if a class defines its own copy constructor, copy-assignment operator, or destructor, the move constructor and move-assignment operator are not synthesized. —— quote from "13.6.2. ...
3
votes
5answers
133 views

Can encapsulation be implemented by proper types rather than accessors?

Interesting question came up while designing interfaces at work, now resolved, but I want to ask about the theory behind it. Is it incorrect to say that properly typed data members of a class provide ...
3
votes
4answers
302 views

How to illustrate to students why they should use the standard library? [closed]

Too often I run into students who: Use arrays instead of vectors, thinking that it's "faster" Use pointers because it's "faster" Reinvent-the-wheel, often copying-pasting terrible C-ish code from ...
7
votes
3answers
539 views

C++ Header files design : just like defining an API?

I'm kind of new to large scale software development in C++, and I was wondering on the design side of things. I was reading this question, and I thought that, overall, once we get past constant ...
3
votes
0answers
491 views

Is it rational to convert protobuf into json to send it to a web server?

Protobuf is a good thing, it allows c++ devs to not bother with class serialization/deserialization, is fast and .proto format is very nice. It also allows automatic data validation. But the messages ...
3
votes
1answer
95 views

Eliminating thread affinity requirements when converting an STA COM object into an MTA COM object

Suppose I need to convert an STA COM object into an MTA object. Single threaded apartments guarantee not just that only one thread will call the object at a time, but that it's always the same thread ...
1
vote
0answers
130 views

Switching from Node.js class techniques to C++

I'm going to be rewriting a project from Node.js into C++11. The device the Node.js scripts are being run on are ARM based, and Node.js is slow to start up on the device. In the Node.js program, I ...
3
votes
2answers
233 views

When is it ok to assert for a pointer being non-null?

This came up as part of a code review for a code segment which resembled this: auto somePikachu = GetMeAPikachu(); NT_ASSERT(somePikachu != nullptr); // this only fires on debug build ...
7
votes
1answer
922 views

static globals and anonymous namespaces in C++

Why did C++ make any distinction between static globals (internal linkage) and symbols in an unnamed namespace (external linkage, but no way to refer to it from outside anyway), when introducing the ...
1
vote
2answers
152 views

Lots of independent data structures into one big one

I have 10 independent data structures that contain unsigned bytes and bit fields from different data sources. I am using C++, and was looking for a way to combine all these data structures into one ...
3
votes
2answers
264 views

Methods of “Code Softening” (Opposite of “Hardening”) for C & C++ [closed]

Some developers work on code hardening. (protecting against errors, you can build with a stack protector for example). However, if you're trying to redo a strange someone else reports, or find ...
3
votes
2answers
361 views

Should I also mock header files?

So, I am part of a very large project which has, as yet, no unit testing (shudder!) and I want to introduce it. I plan on using Google Test and Google mocks. The code is large and unwieldy and ...