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

learn more… | top users | synonyms

0
votes
1answer
151 views

pthread versus boost::thread - need to set the thread priority on an ubuntu box

I wrote a class that basically executes a numerical analysis code in a thread using boost::thread. Now I need to specify the thread priority, since we are running a system with near real-time ...
20
votes
9answers
5k views

Why there is no power operator in Java / C++?

While there is such operator - ** in Python, I was wondering why Java and C++ don't have one too. It is easy to make one for classes you define in C++ with operator overloading (and I believe such ...
0
votes
2answers
99 views

Usage of objects in multiple containers in C++

In cases where there is a desire for objects to be "shared" between multiple containers, I would like to know what is the best practice in C++. For example, when implementing some path algorithms ...
-4
votes
2answers
129 views

Accessing member of vector< pair<pair<int,int>, int> > [closed]

I have made a pair of co-ordinates and pair it with an index in another pair. How can I access members of these vector pairs...?? Is there any better option to implement it..??
0
votes
0answers
33 views

Mocking delegate constructors

I am new to Unit Testing in C++. I am supposed to write a Unit test for my code and I am using Google Mock for the same. I have gone through their turtle example. But I am still confused as to how to ...
61
votes
15answers
14k views

Why would it ever be possible for Java to be faster than C++?

Sometimes Java outperforms C++ in benchmarks. Of course, sometimes C++ outperforms. See the following links: http://keithlea.com/javabench/ ...
1
vote
1answer
515 views

Help me construct a list of best approaches for new C and C++ developers [closed]

Not specific code writing practices. Please also include reasoning. My start: use GCC or Clang gcc because it is unchallenged in the amount of static checking it can do (both against standards ...
2
votes
1answer
129 views

C Module - Where to put prototypes and definitions that do not belong to the public interface?

Since I am mainly an Electrical Engineer, forgive me if I am somewhat off with some terminology. I am currently programming a display driver/interface module. And like so many times before, I have ...
5
votes
6answers
6k views

What should I know about C++? [closed]

I've recently started learning C++, and I enjoy it a lot. I've often read it's easier to write bad code in C++ than in most languages, and that it is a lot deeper than what it seems. As I'd like to ...
4
votes
5answers
3k views

Template Functions and Function templates in C++

I came across a question which asked the difference between Function Templates and Template Functions in C++ and also Template Classes and Class Templates. I was under the impression that Function ...
0
votes
1answer
190 views

Are ANSI* Escape Sequences Proper For Coloring Text in Terminal(Linux)?

I just switched back to Linux after a few years of Windows use and am wondering if ANSI Escape Sequences are okay for coloring text. I ask because the way I did it on windows isn't capable of crossing ...
9
votes
4answers
507 views

Is Non-Deterministic Resource-Management a Leaky Abstraction?

From what I can see, there are two pervasive forms of resource-management: deterministic destruction and explicit. Examples of the former would be C++ destructors and smart pointers or Perl's DESTROY ...
1
vote
3answers
3k views

Is there still any value in learning assembly languages today? [closed]

Specifically for a game programmer. If you really needed some assembly routines you could look for help, whereas back in the 80s/90s it was one of the mainstream languages. I read that compilers can ...
61
votes
11answers
8k views

Why is base-for-all-objects discouraged in C++

Stroustrup says "Don’t immediately invent a unique base for all of your classes (an Object class). Typically, you can do better without it for many/most classes." (The C++ Programming Language Fourth ...
7
votes
4answers
1k views

Avoid having an initialization method

I have this existing code where they have a class and an initialization method in that class. It is expected that once the object of the class is created, they need to call initialize on it. Reason ...
1
vote
1answer
153 views

Best solution to switch between new and deprecated code

We have a feature that is currently implemented, let's call it implA. ImplA works but can't do many things. So we have decided to rewrite it. But we can't just replace it right away, so we will need ...
1
vote
1answer
88 views

Are There Tools for Statically Evaluating C++ Functions for Side Effects? [closed]

I'm interested in having a tool that can evaluate whether a function in C++ has side effects. I'd like to be able to mark functions as having or not having side effects, which I think would make ...
0
votes
0answers
101 views

Member vs free function [duplicate]

I've read similiar topics and also the following great article: http://www.gotw.ca/publications/mill02.htm I'm still not quite sure how to apply it though. Basically I have a class that is an ...
1
vote
4answers
171 views

const reference and const pointer. How do they work?

I read a post about how const storage works. How does const storage work? (Item 2, Scott Myers Effective C++) This says that each segment has separate section of write protected memory and const ...
1
vote
2answers
129 views

How can I have objects interacting and communicating with each other without forcing a hierarchy?

I hope these ramblings will make my question clear — I'd totally understand if they wont, though, so let me know if that's the case, and I'll try making myself clearer. Meet BoxPong, a very ...
17
votes
5answers
3k views

Why do all <algorithm> functions take only ranges, not containers?

There are many useful functions in <algorithm>, but all of them operate on "sequences" - pairs of iterators. E.g., if I have a container and like to run std::accumulate on it, I need to write: ...
14
votes
7answers
1k views

When NOT to use virtual destructors?

I believed I searched many times about virtual destructors, most mention the purpose of virtual destructors, and why you need virtual destructors. Also I think in most cases destructors need to be ...
6
votes
5answers
3k views

Alternate string formatting options in C++?

I'm looking at optimizing some string formatting code that's hit a lot in our code. We had been using ostringstream, and I converted the code to use sprintf (actually Microsoft's more secure ...
5
votes
2answers
510 views

Is it possible to refactor inheritance to composition when virtual methods are called inside the base class?

Let's say I have a class called Country and two subclasses called AI and Player. The Country class has a number of virtual methods to allow player-specific or AI-specific behavior. I want to make it ...
26
votes
1answer
2k views

Why does C++ not have a “pure” keyword for virtual functions?

I have always wondered why we code virtual void MyFunction() = 0; and not pure virtual void MyFunction(); Is there a reference for the basis of this decision?
16
votes
2answers
3k views

What's RAII? Examples?

Always when the term RAII is used, people are actually talking about deconstruction instead of initialisation. I think I have a basic understanding what it might mean but I'm not quite sure. Also: is ...
23
votes
3answers
3k views

C++ strongly typed typedef

I've been trying to think of a way of declaring strongly typed typedefs, to catch a certain class of bugs in the compilation stage. It's often the case that I'll typedef an int into several types of ...
0
votes
4answers
20k views

2 Dimensional Arrays in C++

I started learning arrays in C++ and came over a little side note in the book talking about 2D arrays in breif. I tested it out and i was amazed that it could give the programmer the ability to store ...
3
votes
3answers
210 views

Is this a good method to create class hierarchies?

Is it a good design to store the type of the object in the base class as an enum? For example, consider the following hierarchy Expr --Unary --Binary --Const ----Int ----Float How to represent the ...
2
votes
2answers
214 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 ...
0
votes
3answers
247 views

What arguments are there to use a coding style for each distinct language? [duplicate]

I recently had a discussion about our coding style for C# projects. Two things in particular were very hard to agree upon. Method Naming C# has the de-facto standard of naming (at least public, ...
0
votes
3answers
202 views

Is there a use for non-const reference parameters?

Given a function like: void do_stuff( Thing & thing ) { // at this point, I can inadvertently or purposefully change thing } Is there a reason to purposefully change thing? Changing thing ...
15
votes
5answers
5k views

If a variable has getter and setter, should it be public?

I am an about to graduate Computer Science student so probably this is a stupid question. If I have a class with a variable that is private and the class have getter and setter for that variable. Why ...
2
votes
3answers
206 views

Disallow non-local storage of an object

I need to write a class in C++ that acquires a hardware resource and releases it again when it is destroyed. Basically that can be done in the constructor and destructor of the class. The difficulty ...
2
votes
1answer
138 views

Does the LGPL apply if I rewrite a portion of JasperReports in C++?

I would like to rewrite a portion of the JasperReports library in C++. The library will read from the same XML format used by JasperReports. If I integrate this C++ library to my proprietary ...
3
votes
1answer
240 views

How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: ...
0
votes
2answers
88 views

Should I set values using the return or by giving a reference?

Suppose I have a mouse position watcher, which should let the user extract the mouse position. Should the mouse position be set in the method and returned: Mouse_Position ...
3
votes
1answer
278 views

In C++, why does the main function use 'char *argv[]'?

I was wondering why C++ uses the following function prototype for the main() function: int main(int argc, char *argv[]); instead of something like: int main(std::vector<std::string> argv); ...
7
votes
3answers
10k views

What is meant by Scope of a variable?

I think of the scope of a variable as - "The scope of a particular variable is the range within a program's source code in which that variable is recognized by the compiler". That statement is ...
1
vote
1answer
95 views

A little confused with virtual functions

Please see the following code: #include<iostream.h> #include<string.h> class father { char fname[20]; public: father() { strcpy(fname,"eshwarappa"); ...
0
votes
1answer
62 views

How Do I Create A Module* For A Class That Relies On Another Module?

*I'm using "Module" to mean some extension of a class, either through inheritance or composition. Say I have one of the following declarations of an Entity: using EUnit = int; class Entity : /*With ...
0
votes
0answers
71 views

data structure with best match lookup

Let me explain my question with one example: I have the following structs: struct Box{ unsigned char color; unsigned double width; unsigned double height; unsigned double weight; }; struct ...
2
votes
1answer
100 views

C++: Make classes immutable or compatible with STL containers

Making types immutable is often desireable, especially for multi-threaded applications. There's no need to worry about concurrent access and no need for any synchronization. The common STL containers ...
1
vote
2answers
120 views

“Sweep” a vector of pointers

I'm using a vector to store pointers to objects. In some cases I destroy one or more of these objects (setting the vector spaces to NULL after each delete call), which are externally selected: for ...
0
votes
1answer
147 views

AST processing and usefulness of visitor pattern

I know the visitor pattern is typically used to traverse a hierarchy of heterogeneous objects (inheriting a same abstract object) and dissociate the processing of these objects from the data within ...
2
votes
2answers
546 views

When and why should a destructor be declared as virtual?

As in class TestA { TestA(); virtual ~TestA(); } So why and when should the destructor for TestA be virtual??
6
votes
8answers
780 views

Structuring Procedural vs OO code

I have spent the vast majority of my programming career using Java and very OO based C++. I am really interested in learning to think more procedurally, so I have been starting to do some practice ...
3
votes
1answer
113 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
217 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 ...