Open5

C++ Catch up

nullnull

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;
}
nullnull

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) vs float (64 bit)
    • Accuracy: double > float
    • Speed: double < float
  • enum vs enum 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

nullnull

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