Cpp

From HoerupWiki
Revision as of 19:26, 25 January 2007 by 192.168.10.10 (talk) (C++ standard containers)
Jump to: navigation, search

Der er mange gode artikler om c++ på informit.com. Dr. Dobb's Journal kigger også på mange programmerins-relaterede emner så som C++ views

Hvis at du har brug for noget generic functionality som måske ikke lige ligger i STL er det værd at tage et kig på boost som er en samling af open/ source (licens a la BSD) general purpose klasser/libaries

Hvis at man vil have en lidt enklere tilgang til hukommelses styringen kan man bruge lidt tid på at undersøge RAII (Resource Acquisition Is Initialisation) og brugen af f.eks. std::auto_ptr. Se evt også denne ONLamp artikel

Her er en udemærket artikel omkring exception handling

C++ casting operators

(Citat fra Learning DCOM af Thuan L. Thai)

We all know the power or evilness of the C-style cast, which allows us to cast a given type to practically any other type that we want. For example, we can cast a human into an ant. This can be extremely dangerous, because it can create uncontrollable and error-prone software. To migitate casting problems, C++ allows us to specifically indicate the kind of cast that we want to apply. Check with compiler documentation for the details of the C++ casting operators. In a nutshell, here they are and their short meanings.

  • dynamic_cast
    • Used to convert polymorphic types. Runtime checks will be made to ensure the validity of the cast. If the cast is not safe, a bad_cast exception will be thrown.
  • static_cast
    • Used to convert nonpolymorphic types. No runtime check is involved.
  • const_cast
    • Used to cast away const-ness or volatile-ness of an object.
  • reinterpret_cast
    • Used to cast any pointer or integral type into another pointer or integral type. This is the most flexible and the most dangerous of the C++ casting operators, second only to the traditional C casting operator.

In the old days, we would do the following to cast a double pointer into a void**: (void**)(&pSome)

If we wanted to use the C++ casting operators, we would write the above code as follows:

reinterpret_cast<void**>(&pSome)

The syntax of the other C++ casting operators follows the same pattern as reinterpret_cast.

C++ standard containers

C++ standarten definerer en lang række standart general purpose containere til brug i mange forskellige situationer. Man bør overveje om man kan bruge en af disse inden at man gå i gang med at kode sin egen løsning - ligeledes kan disse være at foretrække frm for containere der kommer sammen med et bestemt toolkit, da C++'s for det første er template baserede og derfor virker på alle former for data og for det andet er man sikker på at std. containerne er tilgængelige hvis man skulle flytte til et andet toolkit / platform.

  • std::vector
  • std::list
  • std::deque
  • std::stack
  • std::queue
  • std::set
  • std::map

TR1 (men ikke ISO) containers:

  • std::hash_set
  • std::hash_map