Questions tagged [c++]

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

Filter by
Sorted by
Tagged with
2
votes
5answers
252 views

Splitting class responsibilities without exposing private data

I'm struggling to find good ways to split up classes without exposing private data. Most articles I read about SRP seem to ignore how the new classes that take on the separated responsibilities access ...
1
vote
2answers
218 views

How to declare and share a lot of variables to provide best performance

My question is not about dilemma between clean code vs performance, but I want to understand exact issue with declaring variables and sharing them between functions. I read in many threads, that from ...
1
vote
0answers
54 views

External standalone cpp code in my project

I've inherited a project that has a codebase in src/, but it also has precompiled binaries of dependency software in bin/. I would like to move away from having precompiled software as part of our ...
5
votes
2answers
216 views

Where can I put project dependencies

I've just finished my studies in Computer Science and now I'm working. The problem is I'm the only computer scientist in my company and I'm probably taking a lot of bad habits. I would like to correct ...
0
votes
2answers
407 views

Logically, is there a reason why ++i++ can not be a valid expression?

I had to increment my integer twice in a loop, so I thought I would try and be clever: for (int i = 0; !sl.isEmpty(); ++i++) { ... } ++i++ however is not an assignable expression, at least in GCC. ...
3
votes
2answers
234 views

Am I using namespaces wrong?

I'm obsessed with organization - it's probably the real reason why I enjoy coding. So I namespace everything. But I'm just curious if I'm doing it wrong by being redundant. Consider this, which I ...
6
votes
1answer
203 views

Is it practical to pass function pointers to a separate class to avoid additional includes?

In my program I've included the header for an external library (GLFW) in my Main class. Everything I need to use that library for can be handled in my main class, with the exception of two sneaky ...
6
votes
4answers
290 views

How to explicitly inform users of the class that calling a method will invalidate the object it was called upon?

Consider this snippet: class Foo { int m_fileDescriptor; public: Bar transformIntoBar() { Bar bar(m_fileDescriptor); m_fileDescriptor = -1; return bar; } }; ...
1
vote
2answers
89 views

const function parameters and default behavior

Say I have a C++ function /** * @param path If empty, the system default is used */ void foo(const std::string& path); And in my implementation I have a default handling for empty paths void ...
8
votes
8answers
666 views

Relevance of optimization techniques

I've heard of some techniques to optimize code and make it faster: On one side are clearly relevant optimization: use of better algorithms, benchmarking, etc. On the other side are techniques with a ...
0
votes
0answers
59 views

Different approaches to plugin system

Having an application that defines a plugin API, I was wondering how (if possible at all) to achieve both stability and performance (see below for what that means) at the same time. A plugin in my ...
1
vote
2answers
445 views

Simple Snake Game in C++

I posted this question originally in Code Review, but then thought that I could possibly get more feedback about the design here. I just finished writing a simple Snake clone in C++ with the goal of ...
0
votes
0answers
32 views

Best practice for installing kernel files alongside OpenCL programs?

OpenCL kernels are compiled at runtime, so have to be installed together with the program (if you are using seperate .cl files, like me). Are there best practices regarding where to install kernel ...
0
votes
4answers
127 views

Design pattern for embedding constructor arguments into classes/structs

I am still quite new on here so I hope I am posting in right forum. I am currently writing a small library where I realized I could use some kind of design pattern which lets one pass constructor ...
24
votes
4answers
5k views

Why is the norm of a complex number in the C++ complex library actually the square of the norm?

In the C++ complex library, the method norm() of a complex number actually returns the square of what I have learned is usually called the "Norm". Reference: std::norm For example, std::norm() of (3,...
1
vote
1answer
91 views

How to simplify work with class inheritance when a lot of arguments needed to be passed to c-tor?

Let's say I have class that used inside another class and uses inside some nested fields of it. To simplify the example I'll use trivial types. In reality it's something like DataType dataType, Status ...
2
votes
0answers
100 views

c++ exceptions, should the caller add the context or provide it to the callee?

Here is a piece of code. Basically, I load some source files, all of them are compiled separately, and then I link all of them. LinkedObject loadObject(std::vector<std::string>& fileNames) {...
-1
votes
2answers
121 views

Developing a C++ program on linux using two threads, always keeping one thread running

The title describes an idea of a solution to a more general problem. Here is the general problem: I am developing a program which looks roughly as follows: [read a lot of data, store in data ...
-2
votes
1answer
99 views

Design a generic hardware interface

I am trying design a generic hardware facade interface for different camera types. below is my incomplete generic interface for camera devices template <class Data,class Configuration> class ...
-1
votes
2answers
107 views

where to put Read/Write threads in c++ classes

I am programming a UART interface (in LINUX) and I want to use a class for that. The way I have it right now is I have a method for write and one for read. The reading is started in a new thread to ...
5
votes
2answers
642 views

Is it good approach to await async tasks in object destructor?

I'm currently working on class with some async tasks under the hood. In fact I need to delegate few tasks to asynchronous execution and be sure that all of them are finished before class was destroyed,...
1
vote
0answers
47 views

Include a database .h file in the factory or in the class in C++

I have a factory, GuiTable.cpp where I create multiple GUI-related classes. I also have a database class, CommonStrDatabase.h which serves as a database file for different strings used in the GUI. ...
0
votes
0answers
88 views

Best design for referring to compile-time set of values at runtime

I am designing an internal API that deals with images. As part of this, I hit this use case where: I specify an image format as a single value. An image format implies a collection of settings (...
4
votes
3answers
497 views

What's the purpose of multiplying by 1024x1024?

I'm working with an old C module that was originally ran on Power PC architecture and compiled with gnu 3.0.6 I'm porting it to run in a VS2012 project on Intel hardware. The module creates a 3D ...
2
votes
1answer
126 views

Optimal design for improved performance of a simulation code

I'm writing a code for the Discrete Element Method where I have balls (spheres) and walls (planes) interacting with each other. Those simulations are run for billions of time steps, so performance ...
1
vote
1answer
186 views

Whats a proper way to design a GUI event system?

I'm trying to write a GUI library for a project (also for learning purposes) from scratch (in C++), and it's working pretty well, but since this is my first attempt i feel that the design of the event ...
4
votes
3answers
306 views

How to adhere to the Open Closed principle in a procedural language like C

In Robert Martin's seminal 1996 article "The Open-Closed Principle" he presents an example in C which does not follow the principle (the DrawAllShapes() method is not closed for modification): enum ...
2
votes
2answers
98 views

Processing user commands and parameters

I'm writing a GUI application which needs the facility for users to interact with the system and execute tasks via a sort of command line interface. The user will type commands in a text box, with ...
1
vote
1answer
303 views

What are the possibilities to embed GIT into application in order to manage version control of the projects that were created by that application?

I am developing an C++ application, which creates tree like structures (collections) of strings. I want to add the ability to save the information of these tree structures remotely/locally (can be ...
25
votes
7answers
6k views

Can abstractions and good code practice in embedded C++ eliminate the need for the debugger?

I am a C developer for an embedded system. YouTube has recently started recommending "C++ for embedded systems" talks. Having watched some of them, they pique my interest, but none of them answer the ...
11
votes
6answers
1k views

why aren't the platform specific integer types in C and C++ (short, int, long) deprecated?

TL;DR: Why isn't everybody screaming, "Don't use short, int, and long unless you really need to, and you very likely don't need to!" I understand that, in theory, by using the types short, int, and ...
4
votes
1answer
91 views

Coupling and shared entity classes

I am currently working on a problem that has a central controller that is handling inputs and outputs between various modules and maintaining a number of entity classes as these change. When working ...
-3
votes
1answer
445 views

How to make code runnable on open source code editors like Ace Editor?

For my computer science uni project I'm required to build a web application with an integrated code editor. I believe I can just integrate open source editors like Ace Editor. However, I feel ...
3
votes
1answer
112 views

Keep track of intializations done in a function

There is an initialization function in my app that does multiple inits. What is a good way to keep a track of inits done so that if init n fails all inits done before it must be cleaned up/de-init ...
1
vote
1answer
133 views

Public class members in PIMPL

I'm attempting to use the PIMPL idiom. This is my public header file in include/foo.h: class FooPrivate; class Foo { public: Foo(); ~Foo(); private: FooPrivate* p_impl; }; ...
1
vote
2answers
138 views

Is there a natural architecture design where I should use mutable member variable(c++)?

I have come across mutable variables kind often, yet it seems to me it's always been use to solve a problem of design(typically coping with legacy code) rather than to answer a natural, intrinsic ...
0
votes
0answers
84 views

C++ client-server design

The context I need to design a Client-Server(-Client) network in C++ that would allow an android application to communicate to a Raspberry Pi and back I am running everything on linux environment(s) ...
5
votes
2answers
247 views

How to best unify different data formats?

I have a hardware device connected to the computer and on the hardware device there is 512 bytes of memory for storing parameters. I want to read this data and decode the parameters stored in the ...
1
vote
4answers
1k views

How to use Macros in Programming to make code faster, efficient and compact

Recently I was going through some of the source-codes of the best competitive programmers in the world. I found out that those people use a template while writing programs, preferably in C++. I have ...
5
votes
6answers
1k views

Is there a way to speed up a big switch statement?

As a practice I'm working on a CPU simulator (runs at about 1.78MHz) and I'm using a switch statement to execute correct opcodes based on the value in the IR (instruction register) variable. This ...
-1
votes
2answers
64 views

OOP classes hierachies for aiding static checking justified?

upfront I develop software for big data applications of which use is beyond my control (it ends up with a user), but I am not a trained software developer. So I apologize for any wrong terminology. ...
3
votes
1answer
110 views

(C++) Ideal design pattern for associating requests/responses on socket?

I'm working on my first big C++ project and while I understand the language I'm trying to learn elegant design patterns using it. I have some low-level networking over a UDP sockets API where each ...
2
votes
4answers
664 views

Why doesn't C++ support covariance in STL containers like C# or Java?

The Covariance and Contravariance feature is well supported in C# and Java collections. However C++ doesn't support them in their STL containers. Why is it so? For example the below code will ...
42
votes
3answers
15k views

Why is the logical NOT operator in C-style languages “!” and not “~~”?

For binary operators we have both bitwise and logical operators: & bitwise AND | bitwise OR && logical AND || logical OR NOT (a unary operator) behaves differently though. There is ~ ...
3
votes
2answers
251 views

How can I cleanly handle deeply nested namespaces in C++?

The first language that I truly learned was Java. In it, it is very syntactically easy to nest classes in an essentially arbitrarily complex package hierarchy, which keeps the code organized. It is ...
52
votes
11answers
11k views

Why does C++ have 'undefined behaviour' (UB) and other languages like C# or Java don't?

This Stack Overflow post lists a fairly comprehensive list of situations where the C/C++ language specification declares as to be 'undefined behaviour'. However, I want to understand why other modern ...
1
vote
1answer
78 views

Simple question about terminal memory [closed]

I'm running a program in C++ on a Chromebook Penguin terminal. The program runs perfectly the first time, but on consecutive runs it acquires different results. I suspect that this is because the ...
-4
votes
1answer
131 views

Is it really good idea to disable warning of C or Cpp? [duplicate]

When we are working on a big project of C or CPP, we are getting some warning. Therefore, we try to remove that warning because when we see compiler warnings that they make us feel dirty. When we ...
3
votes
3answers
825 views

C++ Recursive Descent Parser: Global Variable Dilemma

I'll come straight to the point. I'm trying to create a Recursive Descent Parser in C++ for a hobby project which involves creating my own minimalist programming language. One thing that puzzles me, ...
1
vote
1answer
102 views

Common methodology for organizing C++ source/header/library/resources? [duplicate]

I'm currently working on a C++ project in Visual Studio and can't make up my mind on how I should be organizing my files. As of currently, my file directory looks similar to the following. ->...

1 2
3
4 5
50