Skip to main content

Questions tagged [c++]

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

Filter by
Sorted by
Tagged with
59 votes
9 answers

Explanation on how "Tell, Don't Ask" is considered good OO

This blogpost was posted on Hacker News with several upvotes. Coming from C++, most of these examples seem to go against what I've been taught. Such as example #2: Bad: def check_for_overheating(...
Pubby's user avatar
  • 3,390
254 votes
12 answers

Why do many exception messages not contain useful details?

It seems there is a certain amount of agreement that exception messages should contain useful details. Why is it that many common exceptions from system components do not contain useful details? A few ...
Martin Ba's user avatar
  • 7,637
163 votes
12 answers

When to use C over C++, and C++ over C?

I've been introduced to Computer Science for a little over a year now, and from my experience it seems that C and C++ are both considered to be "ultrafast" languages, whereas others such as Python and ...
88 votes
11 answers

Did the developers of Java consciously abandon RAII?

As a long-time C# programmer, I have recently come to learn more about the advantages of Resource Acquisition Is Initialization (RAII). In particular, I have discovered that the C# idiom: using (var ...
JoelFan's user avatar
  • 7,091
169 votes
18 answers

Is there any reason to use C++ instead of C, Perl, Python, etc.? [closed]

As a Linux (server side) developer, I don't know where and why should I use C++. When I'm going for performance, the first and last choice is C. When "performance" isn't the main issue, programming ...
63 votes
10 answers

Readability versus maintainability, special case of writing nested function calls

My coding style for nested function calls is the following: var result_h1 = H1(b1); var result_h2 = H2(b2); var result_g1 = G1(result_h1, result_h2); var result_g2 = G2(c1); var a = F(result_g1, ...
Dominique's user avatar
  • 1,788
82 votes
12 answers

What's the benefit of object-oriented programming over procedural programming?

I'm trying to understand the difference between procedural languages like C and object-oriented languages like C++. I've never used C++, but I've been discussing with my friends on how to ...
niko's user avatar
  • 2,119
51 votes
5 answers

How could the first C++ compiler be written in C++?

Stroustrup claims that Cfront, the first C++ compiler, was written in C++ (Stroustrup FAQ). However, how is it even possible that the first C++ compiler be written in C++? The code that makes up the ...
Pacerier's user avatar
  • 4,993
33 votes
9 answers

Can C++ be used as a server-side web development language? [closed]

I'd like to get into web development using C++ as the "scripting language" on the server-side. My server infrastructure is *nix based, so doing web development in C++ on Azure is not applicable and C+...
Scott Davies's user avatar
230 votes
8 answers

When do you use float and when do you use double

Frequently, in my programming experience, I need to make a decision whether I should use float or double for my real numbers. Sometimes I go for float, sometimes I go for double, but really this feels ...
Jakub Zaverka's user avatar
147 votes
15 answers

Why did memory-managed languages like Java, Javascript, and C# retain the `new` keyword?

The new keyword in languages like Java, Javascript, and C# creates a new instance of a class. This syntax seems to have been inherited from C++, where new is used specifically to allocate a new ...
59 votes
11 answers

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 ...
Sisir's user avatar
  • 858
43 votes
3 answers

What is message passing in OO?

I've been studying OO programming, primarily in C++, C# and Java. I thought I had a good grasp on it with my understanding of encapsulation, inheritance and polymorphism. One frequently referenced ...
Tom's user avatar
  • 852
197 votes
40 answers

My Dad is impatient with the pace of my learning to program. What do I do? [closed]

So my Dad bought me 5 books on programming (C++, Java, PHP, Javascript, Android) about a month ago. He's an architect and he knows NOTHING about programming. He bought me them because I told him ...
120 votes
11 answers

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 ...
Anonymous's user avatar
  • 3,556
58 votes
5 answers

Where should I put functions that are not related to a class?

I am working on a C++ project where I have a bunch of math functions that I initially wrote to use as part of a class. As I've been writing more code, though, I've realized I need these math functions ...
coconut's user avatar
  • 683
28 votes
11 answers

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++)...
Mateen Ulhaq's user avatar
27 votes
7 answers

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

I have a class with a variable that is private and the class has a getter and a setter for that variable. Why not make that variable public? The only case I think you have to use getters and setters ...
Oni's user avatar
  • 957
24 votes
7 answers

Are header files actually good? [closed]

I find header files to be useful when browsing C++ source files, because they give a "summary" of all the functions and data members in a class. Why do so many other languages (like Ruby, Python, ...
Matt Fichman's user avatar
23 votes
6 answers

Is readability a valid reason to not use const in (reference) parameters?

When writing some functions, I found a const keyword in parameters like this: void MyClass::myFunction(const MyObject& obj,const string& s1,const string& s2,const string& s3){ } ...
ggrr's user avatar
  • 5,783
9 votes
9 answers

What is the way to understand someone else's giant uncommented spaghetti code? [duplicate]

Possible Duplicate: I’ve inherited 200K lines of spaghetti code — what now? I have been recently handled a giant multithreaded program with no comments and have been asked to understand what it ...
Aquarius_Girl's user avatar
151 votes
14 answers

Does auto make C++ code harder to understand?

I saw a conference by Herb Sutter where he encourages every C++ programmer to use auto. I had to read C# code some time ago where var was extensively used and the code was very hard to understand—...
Mircea Ispas's user avatar
  • 1,643
110 votes
13 answers

Should we avoid language features that C++ has but Java doesn't?

Suppose I am limited to use C++ by the environment in the project. Is it good to prevent the use of some language features that C++ has but Java doesn't have (e.g.: multiple inheritance, operator ...
ggrr's user avatar
  • 5,783
46 votes
11 answers

For what reasons should I choose C# over Java and C++? [closed]

C# seems to be popular these days. I heard that syntactically it is almost the same as Java. Java and C++ have existed for a longer time. For what reasons should I choose C# over Java and C++?
Dark Templar's user avatar
  • 6,323
34 votes
3 answers

Did C++11 address concerns passing std lib objects between dynamic/shared library boundaries? (ie dlls and so)?

One of my major complaints about C++ is how hard in practice it is to pass std library objects outside of dynamic library (ie dll/so) boundaries. The std library is often header-only. Which is great ...
Doug T.'s user avatar
  • 11.7k
31 votes
8 answers

Are C and/or C++ viable/practical options for web development? [duplicate]

I am proficient in C, and I am learning C++ right now. I always played with websites (HTML/CSS), and I was wondering if it would be viable/practical to create some simple web apps using C and/or C++. ...
Daniel Scocco's user avatar
31 votes
7 answers

How can I learn to write idiomatic C++?

I am a computer science student, and as a result, I was taught C++ as a better version of C with classes. I end up trying to reinvent the wheel whenever a solution to a complex problem is needed, only ...
yati sagade's user avatar
  • 2,089
29 votes
9 answers

Why should one want to disable compiler warnings?

This answer and the comments added to it show a way to disable several compiler warnings using #pragma directives. Why would one want to do that? Usually the warnings are there for a reason, and I've ...
takrl's user avatar
  • 439
26 votes
5 answers

Why is it bad to write something in language X as if you're writing a program in language Y in terms of using a shared coding paradigm [closed]

A while ago, I asked a question on SO about something written in C++, but instead of getting an answer to the problem at hand, the comments went all crazy on my coding style, even when I indicated ...
Onno's user avatar
  • 1,523
25 votes
5 answers

In C/C++, should I use 'const' in parameters and local variables when possible?

This question is inspired by a question about final in java. In C/C++, should I use const whenever possible? I know there is already a related question about using const in parameters. ...
ocomfd's user avatar
  • 5,712
25 votes
22 answers

Is learning how to use C (or C++) a requirement in order to be a good (excellent) programmer? [closed]

When I first started to learn how to program, real programmers could write assembly in their sleep. Any serious schooling in computer science would include a hefty bit of training and practice in ...
24 votes
4 answers

C++ - Constructor or Initialize Method to Startup [duplicate]

Possible Duplicate: Avoid having an initialization method I want to determine when to do non-trivial initialization of a class. I see two times to do initialization: constructor and other method. ...
Bob Fincheimer's user avatar
23 votes
9 answers

How is C different from C++?

Many people have said that C++ is a completely different language than C, but Bjarne himself has said that C++ is a language that is extended from C hence that is where the ++ comes from. So why does ...
Joshua Partogi's user avatar
21 votes
9 answers

Good design: How much hackyness is acceptable? [duplicate]

Possible Duplicate: Prototyping vs. Clean Code at the early stages I'm right in front of a difficult decision. I have a problem in my codebase (it's in C++), which I could solve in two ways: A) ...
user avatar
20 votes
3 answers

Is there a measure of code rot? [duplicate]

I'm dealing, again, with a messy C++ application, tons of classes with confusing names, objects have pointers into each other and all over, longwinded Boost and STL data types, etc. (Pause and ...
DarenW's user avatar
  • 4,443
19 votes
8 answers

In C and C++, what methods can prevent accidental use of the assignment(=) where equivalence(==) is needed?

In C and C++, it is very easy to write the following code with a serious error. char responseChar = getchar(); int confirmExit = 'y' == tolower(responseChar); if (confirmExit = 1) { exit(0); } ...
DeveloperDon's user avatar
  • 4,978
19 votes
2 answers

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 ...
Felix Dombek's user avatar
  • 2,119
18 votes
7 answers

To rewrite or slowly refactor old C++ project [duplicate]

Our team has recently inherited a relatively large project from another company (~250k lines). It was developed using C++Builder and we intend to port the Ui side to Qt. Most of the Ui code is ...
Boumbles's user avatar
  • 303
12 votes
4 answers

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 ...
user avatar
10 votes
5 answers

Interface and Inheritance: Best of both worlds?

I 'discovered' interfaces and I started to love them. The beauty of an interface is that it is a contract, and any object that fulfills that contract can be used wherever that interface is required. ...
MustafaM's user avatar
  • 2,018
7 votes
1 answer

sqrt c# vs sqrt c++

Any idea why the C# version of sqrt (System.Math.Sqrt) is ~10 times slower than c++ version ? Furthermore, C# version seems to have one extra digit of precision. I have run my test under MSVC2012. I ...
Guillaume Paris's user avatar
5 votes
2 answers

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 ...
user112513312's user avatar
69 votes
7 answers

Why is there no 'finally' construct in C++?

Exception handling in C++ is limited to try/throw/catch. Unlike Object Pascal, Java, C# and Python, even in C++ 11, the finally construct has not been implemented. I have seen an awful lot of C++ ...
Vector's user avatar
  • 3,192
64 votes
3 answers

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 ...
Vorac's user avatar
  • 7,129
58 votes
11 answers

A modern review of Java [closed]

I've been programming for a few years and I began in Java, and in my time I've found many different sources claiming Java to be an inferior language in some way or another. I'm well aware that each ...
Ryan Stull's user avatar
57 votes
15 answers

Why do we have postfix increment?

Disclaimer: I know perfectly well the semantics of prefix and postfix increment. So please don't explain to me how they work. Reading questions on stack overflow, I cannot help but notice that ...
fredoverflow's user avatar
  • 6,884
55 votes
11 answers

What are the best practices regarding unsigned ints?

I use unsigned ints everywhere, and I'm not sure if I should. This can be from database primary key id columns to counters, etc. If a number should never be negative, then I will always used an ...
wting's user avatar
  • 1,034
54 votes
9 answers

Why are pointers not recommended when coding with C++?

I read from somewhere that when using C++ it is recommended not to use pointers. Why is pointers such a bad idea when you are using C++. For C programmers that are used to using pointers, what is the ...
Joshua Partogi's user avatar
53 votes
3 answers

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 ...
Kian's user avatar
  • 633
49 votes
4 answers

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the ...
MdT's user avatar
  • 609

2 3 4 5 6