Questions tagged [c++]
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
262
questions
53
votes
9
answers
20k
views
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(...
252
votes
11
answers
29k
views
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 ...
163
votes
12
answers
512k
views
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
12k
views
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 ...
169
votes
18
answers
201k
views
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 ...
82
votes
12
answers
53k
views
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 ...
63
votes
10
answers
12k
views
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, ...
51
votes
5
answers
37k
views
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 ...
34
votes
9
answers
161k
views
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+...
228
votes
8
answers
293k
views
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 ...
147
votes
15
answers
30k
views
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 ...
58
votes
11
answers
14k
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 ...
43
votes
3
answers
62k
views
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 ...
198
votes
40
answers
20k
views
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 ...
119
votes
11
answers
55k
views
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 ...
57
votes
5
answers
87k
views
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 ...
28
votes
11
answers
38k
views
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++)...
26
votes
7
answers
21k
views
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 ...
23
votes
7
answers
9k
views
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, ...
23
votes
6
answers
10k
views
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){
}
...
9
votes
9
answers
3k
views
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 ...
46
votes
11
answers
393k
views
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++?
34
votes
3
answers
13k
views
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 ...
31
votes
8
answers
36k
views
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++.
...
31
votes
7
answers
10k
views
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 ...
29
votes
9
answers
6k
views
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 ...
26
votes
5
answers
5k
views
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 ...
25
votes
22
answers
16k
views
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
25k
views
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. ...
23
votes
5
answers
15k
views
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. ...
22
votes
9
answers
48k
views
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 ...
21
votes
9
answers
2k
views
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) ...
20
votes
3
answers
2k
views
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 ...
19
votes
8
answers
5k
views
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);
}
...
19
votes
2
answers
6k
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 ...
18
votes
7
answers
3k
views
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 ...
12
votes
4
answers
4k
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 ...
10
votes
5
answers
12k
views
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.
...
7
votes
1
answer
3k
views
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 ...
5
votes
2
answers
4k
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 ...
147
votes
14
answers
46k
views
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—...
111
votes
13
answers
20k
views
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 ...
67
votes
7
answers
59k
views
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++ ...
64
votes
3
answers
47k
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 ...
58
votes
11
answers
16k
views
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 ...
56
votes
15
answers
6k
views
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 ...
55
votes
11
answers
22k
views
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 ...
54
votes
3
answers
27k
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 ...
53
votes
9
answers
51k
views
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 ...
48
votes
16
answers
22k
views
Languages on a resume: Is it better to put "C/C++" or "C, C++"? [closed]
I'm graduating in a couple of weeks, and my resume (as expected) lists the languages that I've had experience with. Previously I've put "C/C++", however back then I didn't have that much experience ...