c++-faq

Best practices for circular shift (rotate) operations in C++

扶醉桌前 提交于 2019-11-25 21:57:31
问题 Left and right shift operators (<< and >>) are already available in C++. However, I couldn\'t find out how I could perform circular shift or rotate operations. How can operations like \"Rotate Left\" and \"Rotate Right\" be performed? Rotating right twice here Initial --> 1000 0011 0100 0010 should result in: Final --> 1010 0000 1101 0000 An example would be helpful. (editor\'s note: Many common ways of expressing rotates in C suffer from undefined behaviour if the rotate count is zero, or

How to stop C++ console application from exiting immediately?

孤街浪徒 提交于 2019-11-25 21:56:31
问题 Lately, I\'ve been trying to learn C++ from this website. Unfortunately whenever I try to run one of the code samples, I see that program open for about a half second and then immediately close. Is there a way to stop the program from closing immediately so that I can see the fruits of my effort? 回答1: Edit: As Charles Bailey rightly points out in a comment below, this won't work if there are characters buffered in stdin , and there's really no good way to work around that. If you're running

What is the curiously recurring template pattern (CRTP)?

試著忘記壹切 提交于 2019-11-25 21:52:58
问题 Without referring to a book, can anyone please provide a good explanation for CRTP with a code example? 回答1: In short, CRTP is when a class A has a base class which is a template specialization for the class A itself. E.g. template <class T> class X{...}; class A : public X<A> {...}; It is curiously recurring, isn't it? :) Now, what does this give you? This actually gives the X template the ability to be a base class for its specializations. For example, you could make a generic singleton

What does the explicit keyword mean?

ε祈祈猫儿з 提交于 2019-11-25 21:43:41
问题 What does the explicit keyword mean in C++? 回答1: The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter. Here's an example class with a constructor that can be used for implicit conversions: class Foo { public: // single parameter constructor, can be used as an implicit conversion

What are all the common undefined behaviours that a C++ programmer should know about? [closed]

妖精的绣舞 提交于 2019-11-25 21:41:38
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. What are all the common undefined

C++: “std::endl” vs “\n”

有些话、适合烂在心里 提交于 2019-11-25 21:41:31
问题 Many C++ books contain example code like this... std::cout << \"Test line\" << std::endl; ...so I\'ve always done that too. But I\'ve seen a lot of code from working developers like this instead: std::cout << \"Test line\\n\"; Is there a technical reason to prefer one over the other, or is it just a matter of coding style? 回答1: The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will

Why do I have to access template base class members through the this pointer?

半城伤御伤魂 提交于 2019-11-25 21:39:31
问题 If the classes below were not templates I could simply have x in the derived class. However, with the code below, I have to use this->x . Why? template <typename T> class base { protected: int x; }; template <typename T> class derived : public base<T> { public: int f() { return this->x; } }; int main() { derived<int> d; d.f(); return 0; } 回答1: Short answer: in order to make x a dependent name, so that lookup is deferred until the template parameter is known. Long answer: when a compiler sees

Why should C++ programmers minimize use of &#39;new&#39;?

时光总嘲笑我的痴心妄想 提交于 2019-11-25 21:39:26
问题 I stumbled upon Stack Overflow question Memory leak with std::string when using std::list<std::string>, and one of the comments says this: Stop using new so much. I can\'t see any reason you used new anywhere you did. You can create objects by value in C++ and it\'s one of the huge advantages to using the language. You do not have to allocate everything on the heap. Stop thinking like a Java programmer. I\'m not really sure what he means by that. Why should objects be created by value in C++

How to pass objects to functions in C++?

≡放荡痞女 提交于 2019-11-25 21:38:54
问题 I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues since we pass just the variable that holds reference to the objects. It would be great if you could also explain where to use each of those options. 回答1: Rules of thumb for C++11: Pass by value , except when you do not need ownership of the object and

What are rvalues, lvalues, xvalues, glvalues, and prvalues?

百般思念 提交于 2019-11-25 21:38:44
问题 In C++03, an expression is either an rvalue or an lvalue . In C++11, an expression can be an: rvalue lvalue xvalue glvalue prvalue Two categories have become five categories. What are these new categories of expressions? How do these new categories relate to the existing rvalue and lvalue categories? Are the rvalue and lvalue categories in C++0x the same as they are in C++03? Why are these new categories needed? Are the WG21 gods just trying to confuse us mere mortals? 回答1: I guess this