c++-faq

What does the C++ standard state the size of int, long type to be?

感情迁移 提交于 2019-11-25 21:38:29
问题 I\'m looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler. But are there any standards for C++? I\'m using Visual Studio 2008 on a 32-bit architecture. Here is what I get: char : 1 byte short : 2 bytes int : 4 bytes long : 4 bytes float : 4 bytes double: 8 bytes I tried to find, without much success, reliable information stating the sizes of char , short , int , long , double , float

What are POD types in C++?

只愿长相守 提交于 2019-11-25 21:37:20
问题 I\'ve come across this term POD-type a few times. What does it mean? 回答1: POD stands for Plain Old Data - that is, a class (whether defined with the keyword struct or the keyword class ) without constructors, destructors and virtual members functions. Wikipedia's article on POD goes into a bit more detail and defines it as: A Plain Old Data Structure in C++ is an aggregate class that contains only PODS as members, has no user-defined destructor, no user-defined copy assignment operator, and

Default constructor with empty brackets

家住魔仙堡 提交于 2019-11-25 21:36:28
问题 Is there any good reason that an empty set of round brackets (parentheses) isn\'t valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type \"()\" automatically everytime. Is there a good reason this isn\'t allowed? 回答1: Most vexing parse This is related to what is known as "C++'s most vexing parse". Basically, anything that can be interpreted by the compiler as a function declaration will be

When can I use a forward declaration?

纵饮孤独 提交于 2019-11-25 21:34:56
问题 I am looking for the definition of when I am allowed to do forward declaration of a class in another class\'s header file: Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ? 回答1: Put yourself in the compiler's position: when you forward declare a type, all the compiler knows is that this type exists; it knows nothing about its size, members, or methods. This is why it's called an incomplete type . Therefore, you

Where do I find the current C or C++ standard documents?

不羁岁月 提交于 2019-11-25 21:34:55
问题 For many questions the answer seems to be found in \"the standard\". However, where do we find that? Preferably online. Googling can sometimes feel futile, again especially for the C standards, since they are drowned in the flood of discussions on programming forums. To get this started, since these are the ones I am searching for right now, where are there good online resources for: C89 C99 C11 C++98 C++03 C++11 C++14 回答1: PDF versions of the standard As of 1st September 2014, the best

What is a smart pointer and when should I use one?

℡╲_俬逩灬. 提交于 2019-11-25 21:34:55
问题 What is a smart pointer and when should I use one? 回答1: UPDATE This answer is rather old, and so describes what was 'good' at the time, which was smart pointers provided by the Boost library. Since C++11, the standard library has provided sufficient smart pointers types, and so you should favour the use of std::unique_ptr, std::shared_ptr and std::weak_ptr. There was also std::auto_ptr. It was very much like a scoped pointer, except that it also had the "special" dangerous ability to be

What is move semantics?

放肆的年华 提交于 2019-11-25 21:34:21
问题 I just finished listening to the Software Engineering radio podcast interview with Scott Meyers regarding C++0x. Most of the new features made sense to me, and I am actually excited about C++0x now, with the exception of one. I still don\'t get move semantics ... What is it exactly? 回答1: I find it easiest to understand move semantics with example code. Let's start with a very simple string class which only holds a pointer to a heap-allocated block of memory: #include <cstring> #include

What are copy elision and return value optimization?

巧了我就是萌 提交于 2019-11-25 21:32:28
问题 What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you\'re probably looking for the introduction. For a technical overview, see the standard reference. See common cases here. 回答1: Introduction For a technical overview - skip to this answer. For common cases where copy elision occurs - skip to this answer. Copy elision is an optimization implemented by most

What is object slicing?

≯℡__Kan透↙ 提交于 2019-11-25 21:30:04
问题 Someone mentioned it in the IRC as the slicing problem. 回答1: "Slicing" is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is "sliced" away. For example, class A { int foo; }; class B : public A { int bar; }; So an object of type B has two data members, foo and bar . Then if you were to write this: B b; A a = b; Then the information in b about member bar is lost in a . 回答2: Most answers here fail to explain what

Why can templates only be implemented in the header file?

£可爱£侵袭症+ 提交于 2019-11-25 21:30:02
问题 Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.) 回答1: It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer. Anyway, the reason your code is failing is that, when