C++ Documentation

website·Indexed 30 days ago

https://cpp.hyperter.top

Guides and documentation for C++, including installation of the GNU Compiler Collection (GCC and G++), system configuration on Ubuntu, and advanced language concepts such as implicit type conversion.

Tokens
1K
Snippets
7
Records
7
Agent score
0%

What's inside C++

  1. Prevent implicit conversions using the `explicit` keyword

    By default, a constructor that can be called with a single argument acts as a conversion operator, allowing the compiler to implicitly convert one type to another. To prevent this and force the developer to use explicit initialization, use the explicit keyword. This is particularly useful for ensuring that function arguments are of the exact intended type and not implicitly converted from another class.
    #include <iostream>
    
    class A {};
    
    class B {
    public:
        // Marking constructor as explicit prevents implicit conversion from A to B
        explicit B(const A& x) {}
        
        B& operator= (const A& x) { return *this; }
        operator A() { return A(); }
    };
    
    void fn(B x) {}
    
    int main() {
        A foo;
        
        // B bar = foo;  // ERROR: Implicit conversion is forbidden by 'explicit'
        B bar(foo);      // OK: Explicit initialization
        
        bar = foo;       // OK: Assignment operator is not affected by 'explicit' constructor
        foo = bar;       // OK: Uses type-cast operator
        
        // fn(foo);      // ERROR: Cannot implicitly convert A to B for function argument
        fn(bar);         // OK
        
        return 0;
    }
  2. Install gcc and g++ on Ubuntu

    To install the GNU Compiler Collection (GCC) and g++ on Ubuntu, use the build-essential package, which includes the compilers and other necessary tools for development.
    sudo apt update
    sudo apt install build-essential
    
    # Verify installation
    gcc --version
    g++ --version
  3. Compile and run a C++ program using g++

    Use g++ to compile C++ source files. It supports various C++ standards (C++11, C++14, C++17, etc.) and handles C++ specific features like classes and templates.
    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Hello, g++!" << endl;
        return 0;
    }
    # Compile the program
    g++ hello.cpp -o hello
    
    # Run the executable
    ./hello
  4. Common gcc and g++ compiler options

    The following flags control the compilation and linking process for both gcc and g++:
    # Compile without linking (generates .o object file)
    gcc -c source.c
    
    # Specify the output executable name
    gcc source.c -o output
    
    # Enable all compiler warnings
    gcc -Wall source.c
    
    # Generate debug information for GDB
    gcc -g source.c
    
    # Add a directory to the include search path
    gcc -I include source.c
    
    # Add a directory to the library search path
    gcc -L /path/to/lib source.c -o output
  5. Compile and run a C program using gcc

    Use gcc to compile C source files into an executable. The -o flag is used to specify the output filename.
    #include <stdio.h>
    
    int main() {
        printf("Hello, gcc!\n");
        return 0;
    }
    # Compile the program
    gcc main.c -o main
    
    # Run the executable
    ./main
  6. Understand implicit numerical type conversion in C++

    Implicit type conversion occurs automatically without explicit operators, such as when passing arguments to functions or returning values. While 'promotion' (e.g., char/short to int, or float to double) typically preserves value, other conversions may introduce errors or warnings:

    • Signed to Unsigned: Negative numbers converted to unsigned types typically use two's complement representation (e.g., -1 becomes 4294967295 for a 32-bit unsigned int).
    • Boolean Conversion: false is equivalent to 0 (numeric) or a null pointer. true is equivalent to any non-zero numeric value; converting true to a numeric type results in 1.
    • Floating-point to Integer: The fractional part is truncated. If the value exceeds the range of the integer type, the behavior is undefined.
    int a = -1;
    unsigned int b = a; // b = 4294967295
    
    int c = -2;
    bool d = c; // d = true
    
    float e = -1.5f;
    int f = e; // f = -1
  7. Understand implicit pointer type conversion in C++

    C++ allows several implicit pointer conversions:

    • Null pointers can be converted to any pointer type.
    • Any pointer type can be converted to a void* pointer.
    • A pointer to a derived class can be converted to a pointer to its accessible base class, preserving const or volatile qualifiers.
    • C-style arrays implicitly convert to a pointer to their first element. This can be used for pointer arithmetic with string literals.
    // "Help" is converted to char* (pointer to first element), then offset by 3
    char* s = "Help" + 3; // s points to 'p'