Contents
Subpages
int*p or int *p?
Bjarne Stroustrup says:
A typical C programmer writes int *p;
and explains it *p
is what is the int
emphasizing syntax.
A typical C++ programmer writes int* p;
and explains it p
is a pointer to an int
emphasizing type. He clearly prefer this emphasis.
See also: StackOverflow difference-between-char-var-and-char-var
Order of #include directives
- h file corresponding to this cpp file (if applicable)
- Other headers from the same project, as needed (Local headers)
- Headers from other non-standard, non-system libraries (for example, Qt)
- Headers from other “almost-standard” libraries (for example, Boost)
- Standard C++ headers (for example, iostream, functional, etc.)
- Standard C headers (for example, cstdint, dirent.h, etc.)
- StackOverflow: c-c-include-header-file-order
- Knatten: the-order-of-include-directives-matter
- Medium: rules-for-managing-header-file-includes-in-c
- Herb Sutter, C++ Coding Standards, Item 23: Make header files self-sufficient
Prefixes on member variables
There are several possibilities in naming member variables.
- Hungarian notation
- prefixes done well (m for member, c for constant, p for pointer, …)
- m_ prefix
- no prefix (Stroustrup, e.g. page 455)
- use this->
- leading (prefix) underscore (may be reserved, Stroustrup page 155)
- postfix underscore (Google C++ Style)
See: stackoverflow why-use-prefixes-on-member-variables-in-c-classes
Note that Python or Ruby enforce the their own prefix, Ruby uses @
for member variables and Python requires self
.
new or new()?
new Thing();
is explicit that you want a constructor called whereas new Thing;
is taken to imply you don’t mind if the constructor isn’t called.
In all versions of C++ there’s a difference between new A
and new A()
because A is a POD.
And there’s a difference in behavior between C++98 and C++03 for the case new B()
.
Do the parentheses after the type name make a difference with new?
void foo() or void foo(void)?
void foo();
Means different things in C and C++! In C it means “could take any number of parameters of unknown types”, and in C++ it means the same as foo(void)
.
- StackOverflow: is-it-better-to-use-c-void-arguments-void-foovoid-or-not-void-foo
- StackExchange: what-is-the-difference-between-function-and-functionvoid
Inheritance class D : B
class derived-class: access-specifier base-class
Default access-specifier is private.
https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
Exception Specifications
Throw keyword in a function signature.
int f() throw();
int g() throw(A,B);
- gotw – A pragm“atic look at exception specifications
- Moral #1: Never write an exception specification.
- Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.
- Exception Specification is deprecated in C++11, and removed in C++17 (p0003r5).
In C++ 11, the noexcept keyword was added as both operator and specifier (cppreference.com). The operator noexcept( expression ) returns a prvalue of type bool.
void f() noexcept; // the function f() does not throw
void (*fp)() noexcept(false); // fp points to a function that may throw
Class Template
As far as C++ is concerned, there is no such thing as a “template class,” there is only a “class template.” (StackOverflow)
Links
- cplusplus.com – C++ Information, tutorials, references and articles.
- C++ Core Guidelines
- Posix – Standard that define a standard operating system interface and environment.
- Standard Template Library Programmer’s Guide
- Variable Initialization – or Is It?
- Microsoft Modern C++ – Articles for writing modern C++ programs.
- GCC5 and the C++11 ABI
- C++0x/C++11 Support in GCC
- C++20
- The Biggest Changes in C++11
- C++ Support in Clang
- GCC Extensions to the C Language Family – For example: case ranges
- cplusplus.com – tutorial type conversions
- stackoverflow – difference between static cast and reinterpret cast
- stackoverflow – when should different cast be used
- Bjarne Stroustrup announces C++ Core Guidelines
- Callback functions in C++