References

Reference vs Pointer to Const

What's the difference between a reference and a pointer to const in terms of function parameters?

3D character art

When it comes to function parameters, both references to const and pointers to const are commonly used for passing objects efficiently without allowing modification. However, there are some key differences in their behavior and use cases.

Let's compare them

Syntax and Usage

In this example, we compare the two types:

#include <iostream>
#include <string>

void PrintByRef(const std::string& str) {
  std::cout << "Reference: " << str << '\n';
}

void PrintByPtr(const std::string* str) {
  if (str) {
    std::cout << "Pointer: " << *str << '\n';
  } else {
    std::cout << "Null pointer\n";
  }
}

int main() {
  std::string message{"Hello, World!"};

  PrintByRef(message);
  PrintByPtr(&message);
  PrintByPtr(nullptr);  // This is valid

  // This would be a compile-time error
  PrintByRef(nullptr); 
}
Reference: Hello, World!
Pointer: Hello, World!
Null pointer

Key differences:

  1. Nullability: Pointers can be null, references cannot. This means you can pass nullptr to a function expecting a pointer, but not to one expecting a reference.
  2. Syntax: References use more intuitive syntax (just like passing by value), while pointers require dereferencing with when accessing the value.
  3. Optionality: Pointers can represent optional parameters (by allowing null), while references always expect a valid object.
  4. Safety: References are generally safer as they can't be null and don't require checking before use.

Performance

In terms of performance, references and pointers are essentially identical. The compiler typically implements references using pointers under the hood.

Const Correctness

Both const T& and const T* prevent modification of the pointed-to object:

#include <iostream>

void ModifyByRef(const int& x) {
  // x++;  // Compilation error
  std::cout << "Reference value: " << x << '\n';
}

void ModifyByPtr(const int* x) {
  // (*x)++;  // Compilation error
  if (x) {
    std::cout << "Pointer value: " << *x << '\n';
  }
}

int main() {
  int value{42};
  ModifyByRef(value);
  ModifyByPtr(&value);
}

When to Use Each

Use references when:

  • The parameter is required and will always be valid.
  • You want a cleaner syntax in the function body.
  • You want to prevent accidental null dereferences.

Use pointers when:

  • The parameter is optional (can be null).
  • You might need to change what the pointer points to.
  • You're working with C-style APIs or older codebases.

In modern C++, references are often preferred for their safety and cleaner syntax, unless you specifically need the nullability or rebinding capabilities of pointers.

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

View Course
Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved