(Click here for a personal note from Marshall Cline.)
C++ FAQ
/
Section 4
/ FAQ 4.9
Section 4:
4.1
Updates for the 03/12 release?
New!
4.2
Updates for the 11/11 release?
New!
4.3
Updates for the 07/11 release?
4.4
Updates for the 05/11 release?
4.5
Updates for the 03/11 release?
4.6
Updates for the 08/10 release?
4.7
Updates for the 06/10 release?
4.8
Updates for the 05/10 release?
4.9
Updates for the 04/10 release?
4.10
Updates for the 03/10 release?
4.11
Updates for the 01/10 release?
4.12
Updates for the 09/09 release?
4.13
Updates for the 06/09 release?
4.14
Updates for the 01/09 release?
4.15
Updates for the 03/07 release?
4.16
Updates for the 09/06 release?
4.17
Updates for the 03/06 release?
4.18
Updates for the 10/05 release?
4.19
Updates for the 07/05 release?
4.20
Updates for the 06/05 release?
4.21
Updates for the 05/05 release?
4.22
Updates for the 04/05 release?
4.23
Updates for the 03/05 release?
4.24
Updates for the 02/05 release?
4.25
Updates for the 12/04 release?
4.26
Updates for the 06/04 release?
4.27
Updates for the 08/03 release?
4.28
Updates for the 06/03 release?
4.29
Updates for the 05/03 release?
4.30
Updates for the 04/03 release?
4.31
Updates for the 03/03 release?
4.32
Updates for the 02/03 release?
4.33
Updates for the 01/03 release?
4.34
Updates for the 12/02 release?
4.35
Updates for the 09/02 release?
4.36
Updates for the 06/02 release?
4.37
Updates for the 05/02 release?
4.38
Updates for the 04/02 release?
4.39
Updates for the 03/02 release?
4.40
Updates for the 08/01 release?
4.41
Updates for the 04/01 release?
4.42
Updates for the 07/00 release?
4.43
Updates for the 03/00 release?
4.44
Updates for the 01/00 release?
4.45
Updates for the 10/99 release?
4.46
Updates for the 07/99 release?
4.47
Updates for the 06/98 release?
4.48
Updates for the 05/98 release?
4.49
Updates for the 09/97 release?
4.50
Updates for the 01/97 release?
4.51
Updates for the 11/96 release?
4.52
Updates for the 10/96 release?
4.53
Updates for the 09/96 release?
4.54
Updates for the 08/96 release?
4.55
Updates for the 07/96 release?
4.56
Updates for the 06/96 release?
4.57
Updates for the 05/96 release?
4.58
Updates for the 04/96 release?
4.59
Updates for the 03/96 release?
4.60
Updates for the 09/95 release?
4.61
Updates for the 06/95 release?
4.62
Updates for the 04/95 release?
4.63
Updates for the 03/95 release?
4.64
Updates for the 01/95 release?
4.65
Updates for the 12/94 release?
4.66
Updates for the 11/94 release?
4.67
Updates for the 08/94 release?
4.68
Updates for the 08/94 release?
[4.9] What updates were made for the 04/10 release?
2 New FAQs:
[4.9] What updates were made for the 04/10 release?
(created)
[7.7] Can a method directly access the non-
public
members of another instance of its class?
(created thanks to a suggestion from
Darren Sargent
and Vikas Rangarajan)
63 Changed FAQs:
[2.11] Where can I find the C++ FAQ translated into Polish?
(updated the Polish translation's URL)
[4.26] What updates were made for the 06/04 release?
(updated the Polish translation's URL)
[4.35] What updates were made for the 09/02 release?
(updated the Polish translation's URL)
[8.5] How can you reseat a reference to make it refer to a different object?
(changed
const X*
to
X const*
)
[10.20] What is the "Named Parameter Idiom"?
(changed
const X&
to
X const&
)
[10.21] Why am I getting an error after declaring a
Foo
object via
Foo x(Bar())
?
(changed
const X&
to
X const&
)
[12.2] Why should I worry about "self assignment"?
(changed
const X&
to
X const&
)
[12.3] OK, OK, already; I'll handle self-assignment. How do I do it?
(clarified the "don't go out of your way to make self-assignment fast" paragraphs at the end thanks to a question from
Stan
; and changed
const X&
to
X const&
)
[12.4] I'm creating a derived class; should my assignment operator call my base class's assignment operator?
(changed
const X&
to
X const&
)
[13.1] What's the deal with
operator
overloading?
(changed
const X&
to
X const&
)
[13.10] How do I create a subscript
operator
for a
Matrix
class?
(changed
const X&
to
X const&
)
[13.12] I still don't get it. Why shouldn't my
Matrix
class's interface look like an array-of-array?
(changed
const X&
to
X const&
)
[15.8] How can I provide printing for my
class
Fred
?
(changed
const X&
to
X const&
)
[15.9] But shouldn't I always use a
printOn()
method rather than a
friend
function?
(changed
const X&
to
X const&
)
[15.10] How can I provide input for my
class
Fred
?
(changed
const X&
to
X const&
)
[15.11] How can I provide printing for an entire hierarchy of classes?
(changed
const X&
to
X const&
)
[15.12] How can I open a stream in binary mode?
(changed
const X&
to
X const&
)
[16.17] But the previous FAQ's code is SOOOO tricky and error prone! Isn't there a simpler way?
(changed
const X&
to
X const&
)
[16.18] But the above
Matrix
class is specific to
Fred
! Isn't there a way to make it generic?
(changed
const X&
to
X const&
)
[16.19] What's another way to build a
Matrix
template?
(changed
const X&
to
X const&
)
[16.21] How can I force objects of my class to always be created via
new
rather than as locals or global/
static
objects?
(changed
const X&
to
X const&
)
[16.22] How do I do simple reference counting?
(changed
const X&
to
X const&
)
[16.23] How do I provide reference counting with copy-on-write semantics?
(changed
const X&
to
X const&
)
[16.24] How do I provide reference counting with copy-on-write semantics for a hierarchy of classes?
(changed
const X&
to
X const&
)
[17.11] How do I change the string-length of an array of
char
to prevent memory leaks even if/when someone throws an exception?
(changed
const X&
to
X const&
and
const X*
to
X const*
)
[17.15] What does
throw;
(without an exception object after the
throw
keyword) mean? Where would I use it?
(changed
const X&
to
X const&
)
[18.1] What is "
const
correctness"?
(changed
const X&
to
X const&
and
const X*
to
X const*
)
[18.4] What does "
Fred const* p
" mean?
(changed
const X*
to
X const*
)
[18.5] What's the difference between "
Fred const* p
", "
Fred* const p
" and "
Fred const* const p
"?
(changed
const X*
to
X const*
)
[18.6] What does "
Fred const& x
" mean?
(changed
const X&
to
X const&
)
[18.7] Does "
Fred& const x
" make any sense?
(changed
const X&
to
X const&
)
[18.8] What does "
const X& x
" mean?
(reversed the sense so
X const&
is now common and
const X&
is classic)
[18.9] What does "
const X* x
" mean?
(reversed the sense so
X const*
is now common and
const X*
is classic)
[18.10] What is a "
const
member function"?
(changed
const X&
to
X const&
)
[18.11] What's the relationship between a return-by-reference and a
const
member function?
(changed
const X&
to
X const&
)
[18.12] What's the deal with "
const
-overloading"?
(changed
const X&
to
X const&
)
[18.13] What do I do if I want a
const
member function to make an "invisible" change to a data member?
(changed
const X*
to
X const*
)
[18.15] Why does the compiler allow me to change an
int
after I've pointed at it with a
int const*
?
(changed
const X*
to
X const*
)
[18.16] Does "
Fred const* p
" mean that
*p
can't change?
(changed
const X*
to
X const*
)
[18.17] Why am I getting an error converting a
Foo**
→
Foo const**
?
(changed
const X*
to
X const*
)
[21.12] If
SortedList
has
exactly
the same public interface as
List
, is
SortedList
a kind-of
List
?
(changed
const X&
to
X const&
)
[22.5] How do you define a copy constructor or assignment
operator
for a class that contains a pointer to a (abstract) base class?
(changed
const X&
to
X const&
)
[23.5] When my base class's constructor calls a
virtual
function on its
this
object, why doesn't my derived class's override of that
virtual
function get invoked?
(changed
const X&
to
X const&
)
[23.6] Okay, but is there a way to
simulate
that behavior
as if
dynamic binding worked on the
this
object within my base class's constructor?
(changed
const X&
to
X const&
)
[26.13] What should be returned from a function?
(changed
const X*
to
X const*
)
[27.10] Are there any lint-like guidelines for C++?
(changed
const X&
to
X const&
)
[27.14] Should I use "unusual" syntax?
(changed
const X&
to
X const&
)
[28.4] Should I buy one book, or several?
(fixed a misspelling thanks to
Nigel Colpitts
)
[29.17] Why doesn't my floating-point comparison work?
(added
#include <cmath>
to make sure the compiler uses the
std::abs(double)
overload, rather than
std::abs(int)
)
[32.5] How can I call a non-system C function
f(int,char,float)
from my C++ code?
(changed
const X*
to
X const*
)
[33.13] What the heck is a functionoid, and why would I use one?
(changed
const X&
to
X const&
)
[33.14] Can you make functionoids faster than normal function calls?
(changed
const X&
to
X const&
)
[35.2] What's the syntax / semantics for a "class template"?
(changed
const X&
to
X const&
)
[35.7] My template function does something special when the template type
T
is
int
or
std::string
; how do I write my template so it uses the special code when
T
is one of those specific types?
(changed
const X&
to
X const&
)
[35.8] Huh? Can you provide an example of template specialization that doesn't use
foo
and
bar
?
(changed
const X&
to
X const&
)
[35.9] But most of the code in my template function is the same; is there some way to get the benefits of template specialization without duplicating all that source code?
(changed
const X&
to
X const&
)
[35.16] Why do I get linker errors when I use template friends?
(changed
const X&
to
X const&
)
[37.3] How can I find a
Fred
object in an STL container of
Fred*
such as
std::vector<Fred*>
?
(changed
const X&
to
X const&
and
const X*
to
X const*
)
[39.1] How do I convert a value (a number, for example) to a
std::string
?
(changed
const X&
to
X const&
)
[39.2] How do I convert a
std::string
to a number?
(changed
const X&
to
X const&
)
[39.3] Can I templatize the above functions so they work with other types?
(changed
const X&
to
X const&
)
[39.5] What should be done with macros that have multiple lines?
(rewrote to reflect current C++ compilers)
[39.11] How can I create two classes that both know about each other?
(fixed the return type in the prose from
Fred
to
Fred*
thanks to
Mark Ruvald
)