Tag Info

Hot answers tagged

14

I would write the if-statement slightly different, so it is taken when the input is successful. for (;;) { cout << ": "; if (cin >> input) break; cin.clear(); cin.ignore(512, '\n'); } It's shorter as well. Which suggests a shorter way that might be liked by your teacher: cout << ": "; while (!(cin >> ...


13

What you have to strive for is avoiding raw loops. Move the complex logic into a helper function and suddenly things are a lot clearer: bool getValidUserInput(string & input) { cout << ": "; cin >> input; if (cin.fail()) { cin.clear(); cin.ignore(512, '\n'); return false; } return true; } int ...


12

In General Terms Profound knowledge of any programming language is likely to help you with picking up other languages quicker. That is so partially because programming is a way of thinking more than it is learning syntax. Most programming practices would be true of most other programming languages. That is why people say that you always learn the second ...


11

According to this Wiki Page: In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). So this: if(a,b,x,y){ will only take the value of y into consideration for the ...


10

I don't believe any such extension could be made. The problem is that there doesn't seem to be any way to tell if a brace is the start of an initializer or not. For example, the following code would appear to be ambiguous under your proposed change: if new T{} { hello(); } The two interpretations are: Create a new T with an empty initializer list, and ...


10

It is implementation defined (so you should avoid using them if you care about portability). Some compilers (e.g. GCC) accept $ in identifiers. BTW, the backquote ` is also unused in C++. Notice that early definition of C or C++ languages did not require (and where not used) on systems using ASCII. This explains why some ASCII characters remain ...


9

Unfortunately, no. This is actually a big problem when teaching C++ in programming classes, especially beginner-level classes: you can learn programming principles, or you can learn the C++ language, but the C++ language has too many pitfalls and stupid little gotchas to learn both effectively within the scope of a one-semester class! There are many things ...


9

It's not so much that for(;;) is bad. It's just not as clear as patterns like: while (cin.fail()) { ... } Or as Sjoerd put it: while (!(cin >> input)) { ... } Let's consider the primary audience for this stuff as being your fellow programmers, including the future version of yourself who no longer remembers why you stuck the break at the ...


9

Since if/else is similar to a switch and often interchangeable, your confusion is understandable. Some languages, such as Python, don't even have a switch statement. Not surprisingly, when you Google for "python switch", the first result points to an alternative—a map. Not an if/else, but a map. While you can use if/else every time instead of a switch, you ...


7

No. Knowing C++ well will make it easier to learn other languages which are like C++. But that would be boring. Why would you want to know two languages which are the same? That doesn't buy you anything. (Note that this isn't specific to C++. It applies to any language. Compare with natural languages: learning Italian will not make it easier to learn other ...


7

As long as the SetPropertyOfItem method reliably doesn't change the property when it detects a conflict, I would always choose the first way of doing things. Separating checks from actions can work, but it's prone to misuse by callers who perform the action without performing the check. And folding the check into the action method can lead to wasted effort ...


6

What you should worry about is ownership of the data and how long the pointers should/will be valid. Using std::vector will allow you to be more relaxed about validity though you should still be careful as a reallocation will invalidate the pointer. However much C++ code is littered with &vector[0] to get the pointer to the first element so it's very ...


6

Parsing full C++ is very hard. And in a large enough code, nearly all of C++ syntactical features are used (since the hard to parse features occur in most C++ code). Also, what is actually parsed is preprocessed output. So you should at least parse the output of the preprocessor. For example, it is quite hard -since very contextual- to understand if an ...


5

I'm going to take a different interpretation here. C++ is a good language for learning about memory and data structures, since it forces you to think carefully about concepts like object ownership and lifespan. And learning about memory and data structures is one of the essential parts of any software engineering curriculum. But you have to be learning ...


5

According to the C++ standard, §4.5 ad. 6 (On integral promotions): A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one. According to the C++ standard, §4.7 ad. 4 (On integral conversions): If the destination type is bool, see 4.12. If the source type is bool, the value false is ...


5

I would say that knowing any programming language deeply will help with learning others, at least other languages in the same family. For example, C++ knowledge won't help much with LISP or Haskell, but for object oriented procedural languages, it will. My approach has been to try and learn a language from each of the different families (procedural, ...


5

To instantiate std::conditional<N==0, val, typename Rec<N-1, val>::type>, the compiler needs to prove that both val and Rec<N-1, val>::type evaluate to a type and that N==0 evaluates to a constant expression that can be used in a boolean context. If those conditions are not met, then the program is not well-formed and requires a diagnostic. ...


5

Short answer: yes. A stream in the context of software is simply a sequence of bytes. What those bytes represent is up to you: it could be ASCII text, Unicode characters, a JPEG image, serialized object, anything your heart desires. The important thing is that the producer and consumer of the stream agree on the format. Furthermore, a stream is not ...


4

There's now a vector.data() method which explicitly blesses the practice


4

Think of decorator as wrapping up an object with more functionality. Now this new object has an object inside of it, and the outer object gives it more functionality. Here's a little pseudo representation of it. class Watch implements Clock { private Clock clockToBeDecorated; public Watch(Clock clock) { clockToBeDecorated = clock; ...


4

I think you might be getting stuck trying to make your class hierarchy fit a real-world taxonomy, and that's not always the best approach. First of all, objects are almost always created by some sort of factory in this situation. You call a probe function that returns a list of all Widgets connected to your system, already instantiated. Then you can ...


4

No, switch statements are not generally used wrong. They are mostly used for their intended use: enumerating action alternatives for a smallish set of possible input values. It's more readable then a long if/else chain, compilers can emit very efficient code if the checked values are reasonably contiguous, and it's easier to write error-checking compilers ...


4

What I do is that I just log the exception class name, the exception message, and the full exception stack trace. I do not think that there is anything else that can be done, nor anything else that needs to be done. I consider this an integral part of the notifier pattern: the fact that an observer may fail should not affect the notifier in any way ...


3

No human or even machine can ever write a parser for a language as incomprehensible as C++. There is no finite set of axiomatic rules in the universe to describe it without contradiction (i.e. bugs). Here is a quote from Steve Yegge's Tour de Babel rant: As for C: it's so easy to write a C compiler that you can build tools on top of C that act like ...


3

My logic instructor at school always said, and pounded it into my brain, that there should only be one entrance and one exit to loops. If not you start getting spaghetti code and not knowing where the code is going during debugging. In real life I feel that code readability is really important so that if someone else needs to fix or debug an issue with ...


3

Cute dynamic programming problem. Here is a Java solution and quick explanation. The basic question to ask yourself is if I knew subproblem X, I could solve solve problem Y easily. In this case, problem Y is the number of substrings divisible by 3, the subproblem X is the number of substrings modulo 3 that terminate at the previous character for each ...


3

What does the sentence "When a class is a resource handle" mean? It means that the classes members are not actually the interesting data, but only pointers, references or other kinds of handles for it. Thus, copying them does not copy the interesting data, meaning a member-by-member-copy is flat-out wrong. Which also answers your other question. For ...


3

The outer loop has n iterations. The middle loop has n iterations. The inner loop has deg(a) iterations. This is because the nxt array is adjacency list of the graph, which means nxt[a] is a list of all edges going from vertex a and therefore nxt[a].size() is (out) degree of a. Because m, the number of edges, is ∑a deg(a), the inner two loops together ...


2

Naming conventions are about supporting human (i.e. programmer and maintainer) comprehension of the code. UDTs can be specified so that declaration of variables, initialisation, expressions, and statements work on them differently than is the case for standard types. For problem finding, it is useful for the maintainer to have some cue that some section ...


2

Pretty much. There's no reason to keep them. I would strip them all out.



Only top voted, non community-wiki answers of a minimum length are eligible