C++ info.

Preprocessor directive

Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

macro definitions (#define, #undef)
Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else and #elif)
Line control (#line)
Error directive (#error)
Source file inclusion (#include)
From: http://www.cplusplus.com/doc/tutorial/preprocessor/

Explicit specifier

Suppose, you have a class String:

class String {
public:
String(int n); // allocate n bytes to the String object
String(const char *p); // initializes object with char *p
};

Now, if you try:

String mystring = ‘x’;

The character ‘x’ will be implicitly converted to int and then the String(int) constructor will be called. But, this is not what the user might have intended. So, to prevent such conditions, we shall define the constructor as explicit:

class String {
public:
explicit String (int n); //allocate n bytes
String(const char *p); // initialize sobject with string p
};

From: https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean

C++ attribute: deprecated

Indicates that the name or entity declared with this attribute is deprecated, that is, the use is allowed, but discouraged for some reason.
Syntax
[[deprecated]] (1)
[[deprecated( string-literal )]] (2)

From: https://en.cppreference.com/w/cpp/language/attributes/deprecated

Friend functions

A friend function of a class is defined outside that class’ scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

From: https://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm

Protected vs. private

private - only available to be accessed within the class that defines them.

protected - accessible in the class that defines them and in other classes which inherit from that class.

From: https://stackoverflow.com/questions/12784083/what-is-the-difference-between-protected-and-private

g++ -c hello_world.cc -o hello_world vs. g++ hello_world.cc -o hello_world

The -c flag tells g++ to compile your source code to object code, but stop short of linking it with the necessary libraries to create a standalone executable binary. From man gcc:

-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.

To create an executable program, simple run your command again without the -c flag:

g++ test.cc -o out

followed by

./out

(the executable flag will be set by default - an explicit chmod should not be required).

From https://askubuntu.com/questions/696764/exec-format-error-of-gcc-compiled-hello-world-c

C macro functions vs. inline functions

  1. Macro functions
    The below is a macro function. It calculates the dot product of two vectors, a and b.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include<stdio.h>

    #define DOT(a, b) \
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2];

    int main() {
    double a[3], b[3];

    a[0] = 1.2; a[1] = 1.3; a[2] = 1.4;
    b[0] = 2.2; b[1] = 2.3; b[2] = 2.4;

    double c = DOT(a, b);

    printf("\nThe value of dot product is: %f\n", c);

    return 0;
    }
  2. Inline functions
    The below is an inline function. It calculates the dot product of two vectors, a and b.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    #include<stdio.h>

    inline double DOT(double a[], double b[]) {
    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
    }

    int main() {
    double a[3], b[3];

    a[0] = 1.2; a[1] = 1.3; a[2] = 1.4;
    b[0] = 2.2; b[1] = 2.3; b[2] = 2.4;

    double c = DOT(a, b);

    printf("\nThe value of dot product is: %f\n", c);

    return 0;
    }

    // For inline functions to work with C99, one has to give the definition
    // in a header file. Or in one compilation unit, one needs to place some
    // sort of "instantiation" without the "inline" keyword
    // (From https://stackoverflow.com/a/9399411)
    // Also it is a good explanation: https://stackoverflow.com/q/6312597
    double DOT(double a[], double b[]);
  3. Why should I use inline functions instead of plain old #define macros?
    The macro functions could cause errors. Inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster. Also unlike macros, argument types are checked, and necessary conversions are performed correctly. (From https://isocpp.org/wiki/faq/inline-functions)

References: https://www.thegeekstuff.com/2013/04/c-macros-inline-functions/
https://stackoverflow.com/a/9399411
https://stackoverflow.com/q/6312597
https://isocpp.org/wiki/faq/inline-functions