c++-faq

Why do we actually need Private or Protected inheritance in C++?

為{幸葍}努か 提交于 2019-11-26 21:19:47
In C++, I can't think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful? It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of composition: the C++ faq-lite gives the following example to illustrate this statement class Engine { public: Engine(int numCylinders); void start(); // Starts this Engine }; class Car { public: Car() : e_(8) { } //

What is the purpose of std::launder?

巧了我就是萌 提交于 2019-11-26 21:17:55
P0137 introduces the function template std::launder and makes many, many changes to the standard in the sections concerning unions, lifetime, and pointers. What is the problem this paper is solving? What are the changes to the language that I have to be aware of? And what are we launder ing? Nicol Bolas std::launder is aptly named, though only if you know what it's for. It performs memory laundering . Consider the example in the paper: struct X { const int n; }; union U { X x; float f; }; ... U u = {{ 1 }}; That statement performs aggregate initialization, initializing the first member of U

Using RAII to manage resources from a C-style API

柔情痞子 提交于 2019-11-26 20:23:50
问题 Resource Acquisition is Initialization (RAII) is commonly used in C++ to manage the lifetimes of resources which require some manner of cleanup code at the end of their lifetime, from delete ing new ed pointers to releasing file handles. How do I quickly and easily use RAII to manage the lifetime of a resource I acquire from a C-style API? In my case, I want to use RAII to automatically execute a cleanup function from a C-style API when the variable holding the C-style resource it releases

gcc/g++: “No such file or directory”

天大地大妈咪最大 提交于 2019-11-26 19:32:06
g++ gives me errors of the form: foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory compilation terminated. It is the same when compiling C-programs with gcc . Why is that? Please note: This question has been asked many times before, but each time it was specific to the askers situation. This question's purpose is to have a question that others can be closed as duplicates of , once and for all; a FAQ . Sebastian Mach Your compiler just tried to compile the file named foo.cc . Upon hitting line number line , the compiler finds: #include "bar" or #include <bar> The compiler

When do extra parentheses have an effect, other than on operator precedence?

孤街醉人 提交于 2019-11-26 19:26:28
Parentheses in C++ are used in many places: e.g. in function calls and grouping expressions to override operator precedence. Apart from illegal extra parentheses (such as around function call argument lists), a general -but not absolute- rule of C++ is that extra parentheses never hurt : 5.1 Primary expressions [expr.prim] 5.1.1 General [expr.prim.general] 6 A parenthesized expression is a primary expression whose type and value are identical to those of the enclosed expression. The presence of parentheses does not affect whether the expression is an lvalue. The parenthesized expression can be

What is the <=> operator in C++?

自古美人都是妖i 提交于 2019-11-26 18:47:52
问题 While I was trying to learn about C++ operators, I stumbled upon a strange comparison operator on cppreference.com, * in a table that looked like this: "Well, if these are common operators in C++, I better learn them", I thought. But all my attempts to elucidate this mystery were unsuccessful. Even here, on Stack Overflow I had no luck in my search. Is there a connection between <=> and C++ ? And if there is, what does this operator do exactly? * In the meantime cppreference.com updated that

Should I include <xxxx.h> or <cxxxx> in C++ programs?

怎甘沉沦 提交于 2019-11-26 18:34:20
What should I include in C++ programs, stdio.h or cstdio ? and Why? Why two header files which provide the same functionality? What does the standard say regarding this? How should I go about including other such headers, Is there a base rule that I should follow? Alok Save Consider the following programs: Sample 1: #include<stdio.h> int main() { printf("Hello World"); return 0; } Sample 2: #include<cstdio> int main() { printf("Hello World"); return 0; } Both work as expected. So which usage is more appropriate? The answer is: Neither! Surprised? Read on. The C++ Standard library provides all

Conversion from Derived** to Base**

混江龙づ霸主 提交于 2019-11-26 17:50:23
问题 I was reading this and unfortunately could not understand in depth why the compiler does not allow conversion from Derived** to Base**. Also I have seen this which gives no more info than the parashift.com's link. EDIT: Let us analyze this code line by line: Car car; Car* carPtr = &car; Car** carPtrPtr = &carPtr; //MyComment: Until now there is no problem! Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++ //MyComment: Here compiler gives me an error! And I try to understand why.

Template specialization of particular members?

假装没事ソ 提交于 2019-11-26 17:32:15
Is it possible to specialize particular members of a template class? Something like: template <typename T,bool B> struct X { void Specialized(); }; template <typename T> void X<T,true>::Specialized() { ... } template <typename T> void X<T,false>::Specialized() { ... } Ofcourse, this code isn't valid. You can only specialize it explicitly by providing all template arguments. No partial specialization for member functions of class templates is allowed. template <typename T,bool B> struct X { void Specialized(); }; // works template <> void X<int,true>::Specialized() { ... } A work around is to

Why are my struct&#39;s members not properly initialised using `{}`? [duplicate]

只愿长相守 提交于 2019-11-26 15:28:50
This question already has an answer here: C and C++ : Partial initialization of automatic structure 5 answers I had the following code: #include <iostream> struct T { int a, b, c; }; int main() { T t = {0}; std::cout << t.a << ',' << t.b << ',' << t.c << '\n'; } Output : 0,0,0 After many years of this code running happily in a critical production environment, serving a vital function, the requirements of the project changed and I needed the output to be 1,1,1 . So, I changed {0} to {1} : #include <iostream> struct T { int a, b, c; }; int main() { T t = {1}; std::cout << t.a << ',' << t.b << ',