Consider this function:

double find_distance (const stud &pnt_1,
const stud &pnt_2);

stud is a defined structure.

What is the point of calling pnt_1 and pnt_2 by reference if 'const' is included at the begginning, making the value remain constant? I mean, when we call by reference, its only to change the variable being called, right?

Hope my question makes sense :lol:

Thx

Dani AI

Generated

Short answer for : taking an argument as a const reference is not pointless. It combines two things you often want at once — you avoid an automatic copy of the object and you also make a compile-time guarantee that the function will not modify the passed object. touched on the efficiency side; the rest below covers useful language and design details.

Two language points that often surprise people:

  • A const reference can bind to temporaries (rvalues), whereas a non-const lvalue reference cannot. That makes it convenient to pass expressions without forcing a copy.
  • When a temporary is bound to a const reference, its lifetime is extended to the lifetime of the reference for that full expression (see the C++ reference on temporary lifetime).

Example (illustrates lifetime extension and a common pitfall):

struct Big { int data[1024]; };

void show(const Big& b);

show(Big()); // OK: temporary bound to const reference for this call

const Big& bad() {
    Big local;
    return local; // WRONG: returns reference to local -> dangling
}

Practical rules of thumb:

  • For small, trivially copyable types (ints, small structs), pass by value; the copy cost is negligible.
  • For large objects, use const T& to avoid copies.
  • In modern C++ (C++11+), consider pass-by-value + move when you need a local copy anyway (see C++ move semantics).
  • Don’t return references to locals; watch object lifetimes.
  • References cannot be null and cannot be reseated; pointers can — choose the one that matches the API semantics.

To clarify : the & in a parameter declaration denotes a reference type (not the address-of operator), and const is a promise about modification, not something inherently required by &. For more on temporary binding and lifetimes, see Temporary object lifetime. For parameter-passing guidance in modern C++, see the C++ FAQ — parameters.

Recommended Answers

All 2 Replies

when you call by reference you also greatly reduce the overhead of creating the callstack of the function.
The amount of stack memory needed will go down from the size of the struct you pass as a parameter to the size of a pointer to that struct.
If the struct is really large (say several kb) that can greatly increase performance and reduce the memory footprint of your application.
By passing a reference you also remove the need to create (underwater) a copy of your data whenever you call a function, another way in which the overhead of creating your callstack is created.

Therefore passing a const reference is almost always preferable if you're going to pass something that's larger than the size of a pointer in your operating system (usually 32 bits in current operating systems).

IMHO ive never HAD to use the const keyword, though i see it in a LOT of & and * parameters so it can only be a good thing. in basic terms it is probably ESSENTIAL for the & operator as that will return the address of the variable, something which really should be constant you might think.... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.