Skip to content

C++ Quick Notes 👷🏼‍♂️👷🏼‍♀️

Classes v Structs

As far as the compiler is concerned there is no difference between structs and classes other than the default access:

  • structs ⇒ default is public
  • classes ⇒ default is private

Not sure about this one -> can split a classs between .h and .cpp files, don't think you can do this with structs.

Structs and Header files

Can include a reference to a struct in the header file, and the rest of it in the implementation file. Such as:

// header file (.h)
struct MyStruct;

// implementation file (.cpp)
struct MyStruct {

    int x;
    int y;

    void MyMethod() {
        // stuff
    }
}

Files that include the header file can only reference or point to the struct, they cannot access its members or its methods. Only the implementation file can access the members and functions of the struct.

Classes and Header Files

dtor's

// header file (.h)
class MyClass {
    ~MyClass();
}

// implementation file (.cpp)
MyClass::~MyClass() {
    // do stuff
}

Exceptions and ctor

Can throw exceptions in ctor, but this limits the object to the scope of the try/ctach block.