Skip to content

Latest commit

 

History

History
80 lines (70 loc) · 8.16 KB

cpp.md

File metadata and controls

80 lines (70 loc) · 8.16 KB

C++

  • Neither C nor C++ are type-safe. One memcpy call and you can put anything in memory meant for some other type.
  • References (e.g. int& something) are C++ only. They are effectively constant final pointers. int& c = a; means "C points to whatever A is already pointing to", which is safer, and simpler to understand.
  • Creating a reference to something uninitialised may accidentally give it value. int a; int& b = a; will suddenly make a nonzero. Use -Wuninitialized (part of -Wall) to detect this error.
  • It is impossible to write int& something;. You must assign it something on initialisation.
  • "Member-access with pointers uses ->; member-access with references uses .
  • The ~ClassName definition in a class denotes a destructor, because ClassName initially is a constructor, but negated by ~ to give the semantic meaning of a destructor.
  • public: followed by an indented block of methods, means that anything inside the block is public (same applies to private)
class Rectangle {
    int width, height;
    public:
        void set_values (int,int);
        int area() {
            return width*height;
        }
};  // semicolon?
  • Trigraphs are character mappings that map three characters to a single one, not found on older keyboards. For example, "??!" maps to "|".
  • You are supposed to use Smart pointers now (C++11), which manages memory allocation for you. You should never (supposedly) create references to objects like in C# and Java either; just create objects and C++ will manage the memory for you.
  • Operators can be overloaded; for example, to overload + for adding two Classes together, use Class operator+(const Class&, const Class&);
  • Function overloading is not a standard feature in C, but you can do it in C11 in an ugly way.
  • Ugly (yet valid) C syntax:
    • Inline return type definitions are possible: struct foo {...} function () { return foo(...) }
    • Returning pointers to functions, where foo takes in nothing, and returns a function bar that takes an int and returns an int: int ( *foo(void) ) (int i) { return bar }
    • "Hello"[5] == 5["Hello"].
  • You can still specify the namespace, foo::some_func(), in a file/method using namespace foo.
  • std::cout is more proper than printf in C++.
  • If your header files are C++ only, name them .hpp. Otherwise, header files that can be used for both C and C++ should use .h.
  • C++ struct values can have defaults:
struct Foo {
    int no_default;
    int yes_default = 0;
};
  • There is struct inheritance. Use the colon to denote structs with all fields in the parent struct:
struct A { };
struct B : A { };  // Has all A's fields
1. `std::string s = std::string("foo");  // creates a temporary std::string object containing "foo", then assigns it to s`
2. `std::string s = "foo";`  // equivalent to 1. Internally, this runs one of the constructors in std::string that accepts const char*.
3. `std::string s = new std::string("foo"); // compiler error while trying to assign a pointer to a variable of type std::string`
4. `std::string s("foo");`

"One of the main benefits of using std::string is that it manages the underlying string buffer for you automatically, so new-ing it kind of defeats that purpose."