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:
    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 by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 491.

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.

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.

Saturday, May 22, 2010

std::string::npos

std::string::npos is defined as '-1', even if string::size_type is 'unsigned int'.

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 by Nicolai M. Josuttis, Addison-Wesley, 1999., p. 495.

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.