answersLogoWhite

0


Best Answer

In C++, the const modifier makes a reference a constant reference.

int i = 42;

const int& r = i;

In the above example, both r and i refer to the same object: a primitive integral with the value 42. That is, r is an alias for i; they are one and the same object. However, the reference r is declared constant so we cannot change the object it refers to by assigning a new value to it. But we can assign a new value to i. And since r and i are the same object, the value of r also changes, despite being decalred constant.

Normally you would not use a constant reference to refer to a local object, since the constness of the reference cannot be guaranteed. However, when we pass an object to a function that expects a constant reference to an object, we give a gurantee to the caller that the object's immutable members will not be changed by the function. This is because the function only has access to the object through the constant reference (formal argument), not the reference we actually passed (actual argument). Therefore it only has access to the constant member functions.

Note that mutable members are nonstatic members that are declared mutable. Typically these will be declared private, for internal use only, such as counters or iterators. As such, modifying a mutable member does not alter an object's external state, as defined by its immutable members. This allows us to invoke constant member functions, which assures us that the object's external state remains unchanged, regardless of the internal state.

Mutable members can also be used to enable lazy construction. That is, an accessor initialises a complex embedded member object when it is first accessed, rather than during construction. This allows the containing object to be constructed much faster, provided the accessor is not invoked as part of the construction process of course. In order for this to work, the embedded member must be a mutable pointer that is initially NULL. When the accessor is invoked for the first time, the complex member is constructed and assigned to the NULL pointer which is then returned (usually by constant reference, but not always). Thereafter, the pointer is no longer NULL and can simply be returned on subsequent accesses. The main advantage of lazy construction is that if the accessor is never called before the containing object falls from scope, we don't waste time and memory constructing an embedded object that was never actually required (and if we had a thousand containing objects, we'd be constructing a thousand embedded objects for no good reason). However, all accessors must be declared constant as they must be accessible to all other member functions, including constant member functions. If the member were not mutable, lazy construction of that member becomes impossible.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What makes a parameter a constant reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Distinguish between constant parameter and time varying parameter system?

A constant parameter is one that does not vary over time, or varies so slowly, or within a very small, allowable range, that it can be used as a constant. One such parameter is the average temperature of the universe, which is 3 Kelvin. We theorize that over time it will gradually decrease with the expansion of the universe, but in practical terms, in a period of billions of years, it is held as a constant. Diurnal light and dark cycles (day time and night time) is an example of a time varying parameter. The intensity of solar radiation, measured in a specific location, varies continuously during each day, and the length of each day varies continously through the year.


Can the parameter of the copy constructor be passed by value?

No. The parameter of the copy constructor is always passed by reference, specifically, a const reference to the class.Think about it. If it were passed by value, then the compiler would already know how to copy the object into the formal parameter, but the purpose of the copy constructor is to provide the code to copy the object, so its kind of a "cart before the horse" thing to think about call by value here.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


Will an attempt to pass a non-variable argument into a reference variable parameter will cause an error?

Yes. Why don't you try it? myfunction(int& a); myfunction(1); // error C2664: 'myfunction' : cannot convert parameter 1 from 'int' to 'int &'


How do you design a function named feetToinches that accepts a number of feet as an argument and returns the number of inches in that many feet?

You design it as an inline function with a constant unsigned reference parameter. Since there are 12 inches to the foot, the return value is the product of the parameter and 12. However, you must also ensure the return type has enough bits to store the result. C++ example: inline unsigned int feetToinches( const unsigned int & feet ){ return( feet < 357913941 ? feet * 12 : 0 ); } The above example assumes an int is 32-bits wide. If the input is larger than 357913941 (feet) then there won't be enough bits to store the result, so zero is returned instead. If the input is non-zero and the return is zero, you know the conversion failed. We use unsigned data types because you cannot have a negative unit of distance. We use a reference parameter to prevent the parameter from being copied (not strictly necessary if the type will fit within a pointer variable, but good practice nonetheless). The parameter is constant because the function does not need to modify the parameter. It is inline because the code is not complicated and is a prime candidate for inline expansion, thus eliminating the need for a function call altogether.

Related questions

Why and when should programmers use a constant reference parameter?

when we use that parameter as a global parameter and we used that parameter through out the program without changing


What are the different parameter passing methods used by c plus plus?

Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).


Why a parameter as a constant?

It is not!


Why a parameter is said to be a constant?

It should not be said to be a constant.


How do you declare a constant?

use: define("GREETING", "Hello you.", true); 1st parameter is the name for your constant 2nd parameter is the value of that constant 3rd parameter is whether or not you want the constant to be case-insensitive. Default is case sensitive. http://php.net/manual/en/function.define.php


What is the difference between ref and out parameters?

Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it. Reference parameter copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.


How can you change values in call by reference?

We don't call by reference, we call functions. The arguments passed to the function are passed (not called) either by value or by reference, depending upon the function signature (the prototype). When you pass by reference you are passing the actual variable, not a copy of the variable, thus the function can modify that variable's value directly. The only exception is when the parameter is declared a constant reference. Passing a pointer is essentially the same as passing by reference, however the pointer itself is passed by value. To pass a pointer by reference you must pass a pointer-to-pointer instead. Passing by value always copies the value, whether it is declared constant or not. But if it is declared constant, the function might as well accept a constant reference. Passing objects (instances of a class) by constant value will incur a performance penalty in making an unnecessary copy. If it is constant, there is little point in copying the object.


What is antonym for parameter?

I don't think there's an antonym for parameter but the synonyms are constant, criterion, framework, guideline, limitation, restriction, and specification.


What is difference between parameter and arbitrary constant?

A parameter is a variable which takes different values and, as it does, it affects the values of some other variable or variables.


Difference between pass by value and pass by reference?

When you pass by value you pass a copy of the name. That is, the function's parameter is assigned the value of the name you pass. If the function parameter is a class type, then that class' copy constructor is called, automatically, which assigns the name's member value(s) to a new instance of the class. The function parameter is temporary, it has local scope, and will therefore fall from scope when the function returns. Thus any changes made to the local name within the function have no effect on the name you passed -- they are completely separate names. When you pass by reference you pass the name itself. To be precise, you pass the memory address of the name, its reference. This address is assigned to the function's reference parameter. Thus any changes made to the reference parameter's value will change the name you passed, since they both refer to the same memory address. Passing by reference is the most efficient method of passing objects (instances of a class) to functions as there is no overhead in copying the object. And if a reference parameter is declared constant then you can be assured the function will not alter the immutable members of the object (while there are ways around this, if a function really intends to alter a reference parameter then it should not be declared constant in the first place). Pass by value should only be used when the value is a primitive data type (generally equal to or smaller than a pointer), or when the function needs to alter the value but must not alter the name that was passed. Note that pointers are always passed by value, but since they can store a memory address they can be treated just as if they were references (assuming the pointer is non-zero of course). When passing pointers, the pointer and the address being pointed at can both be independently declared constant (that is, either, both, or neither can be constant). To pass a pointer by reference you must pass a pointer-to-pointer type, which is itself passed by value. This allows the function to not only modify the address being pointed at (making it point to another address), but to also modify the value at that address, just as if you'd passed the value by reference.


What are the possibe problems if you use call by value instead of call by reference?

When you pass by value you essentially pass a temporary copy of the value. If the value's parameter is declared const, then copying the value could be costly, especially if the value is a large and complex structure or object. If the value's parameter is non-const, then it has to be assumed the function intends to alter the value in some way. If you pass by value, only the copy will be affected, not the original value. When a parameter is declared constant, passing by reference is generally the way to go. When it is non-const, pass by reference if you fully expect any changes to be reflected in the original value, otherwise pass by value.


Distinguish between constant parameter and time varying parameter system?

A constant parameter is one that does not vary over time, or varies so slowly, or within a very small, allowable range, that it can be used as a constant. One such parameter is the average temperature of the universe, which is 3 Kelvin. We theorize that over time it will gradually decrease with the expansion of the universe, but in practical terms, in a period of billions of years, it is held as a constant. Diurnal light and dark cycles (day time and night time) is an example of a time varying parameter. The intensity of solar radiation, measured in a specific location, varies continuously during each day, and the length of each day varies continously through the year.