C&C++

Syntax

declarations specify identifiers, whereas definitions specify objects. An object is defined at the same time it is initialized.

Formatting

modifier Type Conversion
"hh" char integer
"h" short integer
"" signed,unsigned integer
"l" long integer
"ll" long long integer
"j" intmax_t, ``uintmax_t integer
"z" size_t integer
"t" ptrdiff_t integer
"L" long double floating point
flag meaning conversion
"#" alternate form "aAeEfFgGoxX"
"0" zero padding numeric
"-" left adjustment any
" " space for positive number signed
"+" explicit sign for positive number signed

Preprocessor

Constructor

CPP reference

There are two differente ways to create an object

Time t (12, 0, 0);
Time* t = new Time(12, 0, 0);

the first allocates the object on the stack, the latter on the heap (src).

Take in mind that if you want to use the default constructor, you cannot write

Time t();

because would pass as a function declaration, you have to use

Time t;

See this answer from stackoverflow.

Also, when you use a statement like

Time t = Time(12, 0, 0);

in reality, under the hood, the constructor is called of T for the right hand side and then the copy constructor for the assignment to the left hand side.

Default constructor

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.

Copy constructor

It's in the form Class(const Class&).

Inheritance

It's possible to have multiple parent classes.

virtual

  • https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c
  • https://en.cppreference.com/w/cpp/language/virtual

Visibility

They exist three keyword

  • public: anyone can access
  • protected: only class and subclasses can access
  • private: only the class in which is defined can access

but also exists

  • friend: allows to specify methods or attributes that can access private properties

Copying, moving, constant and references

By default, when you pass a class as an argument of a function, the copy constructor is called for that class; to avoid that overhead you can use a const reference.

Operators overloading

Templates

template<typename T>
class Node {
public:
 ...
}

Iterators

Mutables

Interfaces

Pratically a class with only virtual functions.

class Entity {
public:
    virtual std::string getName() = 0;
};

Strings

C++ has the C well known char* with the same behaviour; the "standard" way of using strings in C++ is by the std::string class. Obviously you usually do something like this when coding

void some_function(std::string);

int main() {
    ...
    some_function("foobar");
    ...
}

take in mind that "foobar" is in a readonly memory, under the hood it's converted to std::string copying its content.

Find substring

It exists a special value std::string::npos, from the documentation

This is a special value equal to the maximum value representable by the type size_type. The exact meaning depends on context, but it is generally used either as end of string indicator by the functions that expect a string index or as the error indicator by the functions that return a string index.

std::string::size_type indexU = mOpcode.find('u');

bool isUpper = (indexU != std::string::npos);
int value = 0xbad1d34;
std::cout << std::setfill('0') <<  std::setw(8) << std::hex << value << std::endl;

IO

#include <ifstream>

std::ifstream source("file.auaua", std::ios::in);

while (true) {
    char line[21];

    source.getline(line, 20);

    if (source.fail()) {
        break;
    }
    ...
}

Exceptions

#include <stdexcept>

class DivideDyZero : public runtime_error {
    public:
        DivideDyZero() :
         runtime_error("divide by zero exception") {}
};

void whatever() {
    ...
    try{
        ...
    } catch (DivideDyZero & err) {
        std::cerr << err.what() << std::endl;
    } catch (...) {
        std::cerr << "Catch all" << std::endl;
    }
    ...
}

PIMPL

Idiom to break compilation dependencies.

  • https://www.bfilipek.com/2018/01/pimpl.html
  • https://en.cppreference.com/w/cpp/language/pimpl

Logging

A easy way to add logging in a C++ project is to use plog, you can easily add it as a submodule

#include <plog/Log.h> /* this for all the code that needs the PLOG_* stuff */
#include <plog/Init.h> /* this only for the initialization */
#include <plog/Formatters/TxtFormatter.h>
#include <plog/Appenders/ColorConsoleAppender.h>

int main() {

    /* initialization */
    static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender(plog::streamStdErr);
    plog::init(plog::verbose, &consoleAppender);

    /* code */

    PLOG_INFO << "reading source file '" << argv[1] << "'";
    /* code after */
}