c++-faq

Why doesn't a simple “Hello World”-style program compile with Turbo C++?

馋奶兔 提交于 2019-11-25 23:49:44
问题 I have started learning C++ for my programming class. I have downloaded this \"Hello World\" program: #include <iostream> using namespace std; int main() { cout << \"Hello, World!\"; return 0; } but Turbo C++ complains: Error D:\\HELLO.CPP 1: Unable to open include file \'IOSTREAM\' Error D:\\HELLO.CPP 2: Declaration syntax error Error D:\\HELLO.CPP 6: Undefined symbol \'cout\' What\'s wrong with this very simple program? How can I correct these errors? 回答1: There's no problem with this

What are all the member-functions created by compiler for a class? Does that happen all the time?

丶灬走出姿态 提交于 2019-11-25 23:49:00
问题 What are all the member-functions created by compiler for a class? Does that happen all the time? like destructor. My concern is whether it is created for all the classes, and why is default constructor needed? 回答1: C++98/03 If they are needed, the compiler will generate a default constructor for you unless you declare any constructor of your own. the compiler will generate a copy constructor for you unless you declare your own. the compiler will generate a copy assignment operator for you

How to overload std::swap()

只愿长相守 提交于 2019-11-25 23:43:56
问题 std::swap() is used by many std containers (such as std::list and std::vector ) during sorting and even assignment. But the std implementation of swap() is very generalized and rather inefficient for custom types. Thus efficiency can be gained by overloading std::swap() with a custom type specific implementation. But how can you implement it so it will be used by the std containers? 回答1: The right way to overload swap is to write it in the same namespace as what you're swapping, so that it

Why is it wrong to use std::auto_ptr<> with standard containers?

夙愿已清 提交于 2019-11-25 23:33:43
问题 Why is it wrong to use std::auto_ptr<> with standard containers? 回答1: The C++ Standard says that an STL element must be "copy-constructible" and "assignable." In other words, an element must be able to be assigned or copied and the two elements are logically independent. std::auto_ptr does not fulfill this requirement. Take for example this code: class X { }; std::vector<std::auto_ptr<X> > vecX; vecX.push_back(new X); std::auto_ptr<X> pX = vecX[0]; // vecX[0] is assigned NULL. To overcome

Why aren&#39;t my include guards preventing recursive inclusion and multiple symbol definitions?

笑着哭i 提交于 2019-11-25 23:09:37
问题 Two common questions about include guards: FIRST QUESTION: Why aren\'t include guards protecting my header files from mutual, recursive inclusion ? I keep getting errors about non-existing symbols which are obviously there or even weirder syntax errors every time I write something like the following: \"a.h\" #ifndef A_H #define A_H #include \"b.h\" ... #endif // A_H \"b.h\" #ifndef B_H #define B_H #include \"a.h\" ... #endif // B_H \"main.cpp\" #include \"a.h\" int main() { ... } Why do I get

Is the practice of returning a C++ reference variable evil?

*爱你&永不变心* 提交于 2019-11-25 23:07:14
This is a little subjective I think; I'm not sure if the opinion will be unanimous (I've seen a lot of code snippets where references are returned). According to a comment toward this question I just asked, regarding initializing references , returning a reference can be evil because, [as I understand] it makes it easier to miss deleting it, which can lead to memory leaks. This worries me, as I have followed examples (unless I'm imagining things) and done this in a fair few places... Have I misunderstood? Is it evil? If so, just how evil? I feel that because of my mixed bag of pointers and

C++11 rvalues and move semantics confusion (return statement)

穿精又带淫゛_ 提交于 2019-11-25 23:06:22
问题 I\'m trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); Second example std::vector<int>&& return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return std::move(tmp); } std::vector<int> &&rval_ref = return_vector(); Third example std:

What does T&& (double ampersand) mean in C++11?

折月煮酒 提交于 2019-11-25 23:05:26
问题 I\'ve been looking into some of the new features of C++11 and one I\'ve noticed is the double ampersand in declaring variables, like T&& var . For a start, what is this beast called? I wish Google would allow us to search for punctuation like this. What exactly does it mean? At first glance, it appears to be a double reference (like the C-style double pointers T** var ), but I\'m having a hard time thinking of a use case for that. 回答1: It declares an rvalue reference (standards proposal doc).

Which kind of pointer do I use when?

巧了我就是萌 提交于 2019-11-25 22:59:45
问题 Ok, so the last time I wrote C++ for a living, std::auto_ptr was all the std lib had available, and boost::shared_ptr was all the rage. I never really looked into the other smart pointer types boost provided. I understand that C++11 now provides some of the types boost came up with, but not all of them. So does someone have a simple algorithm to determine when to use which smart pointer? Preferably including advice regarding dumb pointers (raw pointers like T* ) and the rest of the boost

Advantages of using forward

混江龙づ霸主 提交于 2019-11-25 22:56:06
问题 In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues? template <typename T1, typename T2> void outer(T1&& t1, T2&& t2) { inner(std::forward<T1>(t1), std::forward<T2>(t2)); } 回答1: You have to understand the forwarding problem. You can read the entire problem in detail, but I'll summarize. Basically, given the