reference


Reference

In C++, a reference is an alias for an existing variable. It provides an alternative name for a variable and allows you to manipulate the original variable directly. References are widely used in C++ for various purposes, including function parameters, return values, and as alternatives to pointers. This guide will explore different aspects of references through practical examples.

Example 1: Basic Usage of References

#include <iostream>

int main() {
    int original = 42;
    int& ref = original;

    std::cout << "Original value: " << original << std::endl;
    std::cout << "Reference value: " << ref << std::endl;

    ref = 100;

    std::cout << "After modification:" << std::endl;
    std::cout << "Original value: " << original << std::endl;
    std::cout << "Reference value: " << ref << std::endl;

    return 0;
}

Explanation

This example demonstrates the basic usage of references:

Example 2: References as Function Parameters

#include <iostream>
#include <string>

void modifyString(std::string& str) {
    str += " World!";
}

int main() {
    std::string message = "Hello";

    std::cout << "Before function call: " << message << std::endl;

    modifyString(message);

    std::cout << "After function call: " << message << std::endl;

    return 0;
}

Explanation

This example shows how references can be used as function parameters:

Example 3: References and Const

#include <iostream>

void printValue(const int& value) {
    std::cout << "The value is: " << value << std::endl;
    // value = 10; // This would cause a compilation error
}

int main() {
    int number = 42;
    const int& constRef = number;

    std::cout << "Original number: " << number << std::endl;
    std::cout << "Const reference: " << constRef << std::endl;

    number = 100;
    std::cout << "After modifying original:" << std::endl;
    std::cout << "Original number: " << number << std::endl;
    std::cout << "Const reference: " << constRef << std::endl;

    printValue(number);

    return 0;
}

Explanation

This example illustrates the use of const references:

Example 4: References to Temporary Objects

#include <iostream>
#include <string>

std::string getName() {
    return "John Doe";
}

void printName(const std::string& name) {
    std::cout << "Name: " << name << std::endl;
}

int main() {
    // const reference to a temporary object
    const std::string& name = getName();
    std::cout << "Stored name: " << name << std::endl;

    // Passing temporary object to a function expecting a const reference
    printName(getName());

    return 0;
}

Explanation

This example demonstrates how references can be used with temporary objects:

Summary

References in C++ provide a powerful mechanism for creating aliases to existing variables. They offer several advantages:

  1. Improved readability and simplicity compared to pointers.
  2. Efficient parameter passing for functions, avoiding unnecessary copying.
  3. Ability to modify original variables when used as non-const references.
  4. Safety when used as const references, preventing unintended modifications.
  5. Lifetime extension for temporary objects when bound to const references.

Understanding and properly using references is crucial for writing efficient and maintainable C++ code. They are particularly useful in scenarios involving function parameters, return values, and working with complex data structures. However, it's important to be aware of potential pitfalls, such as dangling references, and to use const references when modification is not intended.

Related

Previous Page | Course Schedule | Course Content