Difference between revisions of "Cpp"
(→C++ standard containers) |
|||
(44 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | =C++= | + | =Diverse C++= |
− | Hvis at du har brug for noget generic | + | Der er mange gode artikler om c++ på [http://www.informit.com/articles/index.asp?st=41346 informit.com]. [http://www.ddj.com/ Dr. Dobb's Journal] kigger også på mange programmerins-relaterede emner så som [http://www.ddj.com/dept/cpp/196513737?cid=RSSfeed_DDJ_All 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å [http://www.boost.org/ 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 [http://www.hackcraft.net/raii/ RAII (Resource Acquisition Is Initialisation)] og brugen af f.eks. <code>[http://en.wikipedia.org/wiki/Auto_ptr std::auto_ptr]</code>. Se evt også denne ONLamp [http://www.onlamp.com/pub/a/onlamp/2006/05/04/smart-pointers.html?CMP=OTC-6YE827253101&ATT=Smart+Pointers+in+C artikel] | ||
+ | |||
+ | Her er en udemærket artikel omkring [http://www.informit.com/articles/article.asp?p=461634&seqNum=3&rl=1 exception handling] | ||
+ | |||
+ | http://cppunit.sourceforge.net/cppunit-wiki | ||
+ | |||
+ | ==Code reformatering== | ||
+ | hvis formateringen på en stump kode er helt i spåner, så tag et kig på [http://sourceforge.net/projects/astyle astyle] | ||
+ | |||
+ | =C++ casting operators= | ||
+ | (Citat fra 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. | ||
+ | |||
+ | *<code>dynamic_cast</code> | ||
+ | **Used to convert polymorphic types. Runtime checks will be made to ensure the validity of the cast. If the cast is not safe, a <code>bad_cast</code> exception will be thrown. | ||
+ | *<code>static_cast</code> | ||
+ | **Used to convert nonpolymorphic types. No runtime check is involved. | ||
+ | *<code>const_cast</code> | ||
+ | **Used to cast away const-ness or volatile-ness of an object. | ||
+ | *<code>reinterpret_cast</code> | ||
+ | **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**: | ||
+ | <code> | ||
+ | (void**)(&pSome) | ||
+ | </code> | ||
+ | |||
+ | If we wanted to use the C++ casting operators, we would write the above code as follows: | ||
+ | |||
+ | <code> | ||
+ | reinterpret_cast<void**>(&pSome) | ||
+ | </code> | ||
+ | |||
+ | The syntax of the other C++ casting operators follows the same pattern as reinterpret_cast. | ||
+ | |||
+ | =C++ standard containers= | ||
+ | Flyttet til [[Cpp standard containers]] | ||
+ | |||
+ | =Andre libraries= | ||
+ | *[http://xmlrpcpp.sourceforge.net/ XML-RPC] | ||
+ | *[http://sigslot.sourceforge.net/ sigslot] |
Latest revision as of 06:46, 4 October 2007
Contents
Diverse C++
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
http://cppunit.sourceforge.net/cppunit-wiki
Code reformatering
hvis formateringen på en stump kode er helt i spåner, så tag et kig på astyle
C++ casting operators
(Citat fra 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.
- Used to convert polymorphic types. Runtime checks will be made to ensure the validity of the cast. If the cast is not safe, a
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
Flyttet til Cpp standard containers