Sunday, May 30, 2010
Public Inheritance
If you are deriving from three classes publically, you must specify the keyword 'public' each time (i.e. 3 times); or else the inheritance will default back to private inheritance.
Saturday, May 29, 2010
String Insert Ambiguity
If s is a string, then the following code may not compile:
Note that 0 is a valid size_type and a valid char*.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 491.
s.insert(0,1,' ');because there is an ambiguity between the following two member functions:
insert(size_type idx, size_type num, charT c);
insert(iterator pos, size_type num, charT c);
when iterator is implemented as char*.Note that 0 is a valid size_type and a valid char*.
s.insert(static_cast(0),1,' ');
would fix the ambiguity.Reference: The C++ Standard Library: A Tutorial and Reference
Wednesday, May 26, 2010
Koenig Lookup
Given the following code: getline(std::cin,s); You don't have to prefix getline with std:: because of Koenig lookup.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999., p. 493.
Reference: The C++ Standard Library: A Tutorial and Reference
Monday, May 24, 2010
size_type
It is important to use string::size_type instead of int for return values string member functions; otherwise, it is not portable.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis, Addison-Wesley, 1999 p. 495.
Reference: The C++ Standard Library: A Tutorial and Reference
Saturday, May 22, 2010
std::string::npos
std::string::npos is defined as '-1', even if string::size_type is 'unsigned int'.
For example:
std::string::npos could be a different type, such as int. It depends on the compiler.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis, Addison-Wesley, 1999., p. 495.
For example:
unsigned char npos = -1;is valid a valid statement.
std::string::npos could be a different type, such as int. It depends on the compiler.
Reference: The C++ Standard Library: A Tutorial and Reference
Thursday, May 20, 2010
Dereferenced this pointer
When a ClassA member function returns *this, the following could be the return value type: ClassA& and ClassA.
Tuesday, May 18, 2010
Allocators
DOS Memory Models (tiny, small, medium, large, huge, or flat) were the initial reason allocators were invented.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999., p. 31.
Reference: The C++ Standard Library: A Tutorial and Reference
Sunday, May 16, 2010
Binary Strings
As long as a binary file is not larger than a string can hold, it can be read into a string without any corruption of data, by using the manipulator noskipws.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999., p. 626.
Reference: The C++ Standard Library: A Tutorial and Reference
Saturday, May 15, 2010
Stream's failbit
If a stream's failbit is set, every operation on that stream is a no-op until the failbit is cleared.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 599.
Reference: The C++ Standard Library: A Tutorial and Reference
Wednesday, May 12, 2010
Exceptions
After catching an exception with catch (...){}, you can get the what() data if you rethrow the exception and catch it with an exception type.
Monday, May 10, 2010
cin
The following are member functions of cin: get(), getline(), read() and readsome().
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis Addison-Wesley, 1999, p. 607.
Reference: The C++ Standard Library: A Tutorial and Reference
Saturday, May 8, 2010
Argument Dependent Lookup (ADL)
The following code compiles:
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 613.
#include <iostream>
int main() {endl(std::cout);}
Note that there is no 'std::' in front of endl and endl is being called like a function. Since the parameter to endl specifies the namespace, ADL (Argument Dependent Lookup aka Koenig Look) looks first for the function in the namespace of the parameter, which in this case is std. It is nicer to specify the 'std::' though.Reference: The C++ Standard Library: A Tutorial and Reference
Thursday, May 6, 2010
Stream's imbue() member function
A stream's imbue() member function sets the locale object.
Reference: "The C++ Standard Library"
by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 626.
Reference: "The C++ Standard Library"
Wednesday, May 5, 2010
ios_base flags
ios_base flags to the C Mode flags mapping: in->"r", out or out|trunc->"w", out|app->"a",in|out->"r+",in|out|trunc->"w+"
Reference: C++ Standard Library, The: A Tutorial and Reference, MobiPocket
by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 632.
Reference: C++ Standard Library, The: A Tutorial and Reference, MobiPocket
Sunday, May 2, 2010
Streams
If you close then re-open a stream, the stream's state flags are not cleared.
Reference: The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 634.
Reference: The C++ Standard Library: A Tutorial and Reference
Subscribe to:
Comments (Atom)
