Open5
C++ Catch up
namespace
- shouldn't use
using namespace
- if we use
using namespace
and have methods with the same name in different namespaces, it can cause ambiguity and result in a compilation error.
main.cpp
#include <iostream>
namespace A {
void print() { ... }
}
namespace B {
void print() { ... }
}
int main() {
// wrong
using namespace A;
using namespace B;
print()
// correct
A::print();
B::print();
return 0;
}
Basic Type
- In C++, basically explicit type declaration is recommended
- 👍 Clearly fixed sizes and simple type names
- 📑
int a
->std::int8_t a
-
double (32 bit)
vsfloat (64 bit)
- Accuracy: double > float
- Speed: double < float
-
enum
vsenum class
- 👍
enum class
is typically the safer and cleaner choice.- Scope: prevent name conflicts with other enumerators or variables.
- Type safety: prevent implicit conversions to and from integers.
- Explicit type: Requires explicit casting for conversions
- 👍
Bit
xxx
Variable
-
const
vsconstexpr
- xxx
function
- If we use override, we must use the
override
modifier.- 👍 improve readability
- 👍 improve safity by catching potential errors at compile time.
main.cpp...example