Can I passing const to const reference?

1
2
3
4
5
6
void functionA(const int &number)
{
cout << number;
}

functionA(500);

When functionA receive 500, Where address of "&number" refer to?
You can pass a const to a const reference, but it has to be a memorized value in memory. When you use functionA(500); you are using a literal which doesn't take any memory.

try this

1
2
const int i = 500;
functionA(i);
Stewbond wrote:
You can pass a const to a const reference, but it has to be a memorized value in memory. When you use functionA(500); you are using a literal which doesn't take any memory.


A const reference can bind to a temporary, which is what happens in this case. A temporary int is initialized with the value 500 and fed as a reference to functionA (assuming the call isn't inlined.)


Arrandale wrote:
When functionA receive 500, Where address of "&number" refer to?
& doesn't have anything to do with an address in this context. All it says is that the whatever is passed to functionA will be passed by (const) reference. In other words number will be an alias for whatever variable or value was fed to the function.
Topic archived. No new replies allowed.