Sie sind auf Seite 1von 3

---------- ---------- ---------- ---------- ---------- ----------

1. signature of main()

- according to the standard,


int main()
- implict return 0;
- can have
return EXIT_SUCCESS // defined in #include <cstdlib>
or
return EXIT_FAILURE

---------- ---------- ---------- ---------- ---------- ----------


5. NULL or 0

- in C, NULL is 0 or ((void*)0)
- in C++, NULL is (int)0
- use 0 or nullptr, do not use NULL
- standard: you can always delete a NULL pointer

---------- ---------- ---------- ---------- ---------- ----------


6. prefix or postfix increment

- important for iterator classes, that implement operator overloads


- prefix increment
T& operator++ ()
postfix increment
const T& operator++ (int)
- prefix is more efficient

---------- ---------- ---------- ---------- ---------- ----------


7. class initializer list

- must be used for const and ref data members


- should be used for other data members for efficiency

class::ctor(string& argstr): memstr(astr) { // copy ctor called


}

class::ctor(string& argstr) { // default ctor called


memstr = astr; // assignment operator called
}

- should be sued for other data members - for 'correctness'


principle: initialize everything as early as possible (time)
and at one place (space)

---------- ---------- ---------- ---------- ---------- ----------


8. what is wrong with #define macro constants

#define AGE 100

- macro names are not scoped


- macro names are not typed

---------- ---------- ---------- ---------- ---------- ----------


9. func() or func(void)

- in C++, they are equivalent, but style convention to use


func(void)

- in C, a func() declaration, means arbitrary type and number of args

---------- ---------- ---------- ---------- ---------- ----------


10. avoid C-stye comments /* */

- C-style comments do not nest


- use C++ // comments

---------- ---------- ---------- ---------- ---------- ----------


2. #include header form

- always use
#include <cmath>
- not "math.h" or <math.h>
- system header files
#include <iostream> // C++ standard library headers
#include <cstdlib> // C standard library headers
- all names in the std namespace

---------- ---------- ---------- ---------- ---------- ----------


3. the C++ standard library system headers

exception stdexcept memory new typeinfo utility


limits numeric bitset complex
vector list deque queue stack map set
algorithm iterator functional
ios iostream istream ostream iomanip iosfwd fstream sstream streambuf
locale string

---------- ---------- ---------- ---------- ---------- ----------


3. using "using namespace std"

- the 'using' declation brings the stated namespace into scope


- namespaces are designed to hide names and isolate them from the
'global' namespace
- do not use 'using' in header files

---------- ---------- ---------- ---------- ---------- ----------


4. anonymous namespaces

- provides internal linkage (within the file)

---------- ---------- ---------- ---------- ---------- ----------


11. place #include in header or source file

- as much as possible in source file, to improve build times


- header files carry interface;
in header files, use forward declarations for symbols used
in the interface (e.g. string), instead of including the
entire header (#include <string>)

---------- ---------- ---------- ---------- ---------- ----------


12. Should I declare all the variables used in a function
at the top of that function

- No.
- Principle: mimimize variable scope, tighten scope as much as possible

---------- ---------- ---------- ---------- ---------- ----------


13. convert string to number

double num;
string teststr("14.2");
stringstream converter;
converter << teststr;
converter >> num
-or-
double num = boost::lexical_cast<double>(teststr);

---------- ---------- ---------- ---------- ---------- ----------


14. convert number to string

double num(14.2);
string teststr;
strinstream converter;
converter << num;
converter >> teststr;
-or-
string str = boost::lexical_cast<string>(num);

---------- ---------- ---------- ---------- ---------- ----------


15. inclusion guard for header files

#ifndef MYHEADER_H
#define MYHEADER_H
...
#endif

ESOTERICs:
---------- ---------- ---------- ---------- ---------- ----------
C++ does not allow modification of temporaries of built-in type
vector<T> myvector;

myvector.end()
need not return an object of type T,
instead it may return a temporary of built-in type
so, you cannot use --myvector.end()
instead use
myvector.end()-1
to access the last element of the vector

---------- ---------- ---------- ---------- ---------- ----------


http://www.worldcolleges.info/sites/default/files/CppFAQ.pdf
Section 4.10 How do I tell if a stringstream is empty?

---------- ---------- ---------- ---------- ---------- ----------

Das könnte Ihnen auch gefallen