Questions tagged [pass-by-const-reference]
52 questions
41
votes
5 answers
Why is it allowed to pass R-Values by const reference but not by normal reference?
The following program
void display(const int& a)
{
cout << a ;
}
will work if called with a literal like this
display(5);
but without the const it won't work.
So how can a const reference keep pointing to an R-Value (anonymous variable)?

AnotherOne
- 854
- 2
- 8
- 19
36
votes
4 answers
Modifying const reference argument via non-const reference argument
Consider the following code:
#include
void f(int const& a, int& b)
{
b = a+1;
}
int main() {
int c=2;
f(c,c);
std::cout << c << std::endl;
}
Function f takes two reference arguments: int const& a and int& b. Therefore, f is…

Gabriele Buondonno
- 702
- 7
- 16
33
votes
3 answers
const-ref when sending signals in Qt
This is a thing that I never quite got with const-ref and I really hope that someone could explain it to me.
When calling a function inside of another function, I get that const-ref is the best way when passing stack objects that I don't plan to…

chikuba
- 4,229
- 6
- 43
- 75
20
votes
4 answers
c++ passing by const reference
In the following program body cosists of a vector of pointers. Points is a struct of x,y,z coordinates and a point_id. I believe as body is passed by const reference, the following step should produce an error. BUt the program is running without any…

user829209
- 219
- 2
- 7
18
votes
3 answers
Passing literal as a const ref parameter
Imagine the following simplified code:
#include
void foo(const int& x) { do_something_with(x); }
int main() { foo(42); return 0; }
(1) Optimizations aside, what happens when 42 is passed to foo?
Does the compiler stick 42 somewhere (on…

Super-intelligent Shade
- 5,932
- 23
- 46
15
votes
2 answers
Can I let the C++ compiler decide whether to pass-by-value or pass-by-reference?
Have a look at this hypothetical header file:
template
class HungryHippo {
public:
void ingest(const T& object);
private:
...
}
Now, for a HungryHippo it makes sense that you would want to ingest references to the strings…

Calvin
- 2,872
- 2
- 21
- 31
12
votes
2 answers
Can't pass 'const pointer const' to const ref
Suppose you have a set of pointers (yeah...) :
std::set myTypeContainer;
Then suppose that you want to search this set from a const method of SomeType:
bool SomeType::IsContainered() const
{
return myTypeContainer.find(this) !=…

Thomas B.
- 691
- 4
- 15
11
votes
6 answers
Ampersand & with const in constructor
Can some body tell me the reason why we usually put const and & with some object which is passed in the constructor for example.
Book::Book(const Date &date);
The confusion that i have here is that usually & sign is used in the some function…

Madu
- 4,849
- 9
- 44
- 78
9
votes
4 answers
When is an object sufficiently large that there is a performance gain in passing it by reference instead of by value?
As answered in this question by Charles Bailey pass by constant reference should be considered when the object type is large but what kind of object is considered large?
EDIT:
OK for providing more data that could be used to provide a more concrete…

max
- 2,627
- 1
- 24
- 44
7
votes
3 answers
Passing std::shared_ptr by value or const ref and then storing in a container?
Consider the following vector:
std::vector> myVector;
and the following two functions that add a given element to the vector:
void foo1(std::shared_ptr x)
{
myVector.push_back(x);
}
void foo2(const std::shared_ptr&…

ksl
- 4,519
- 11
- 65
- 106
5
votes
4 answers
how does std::string manages this trick?
i just wrote a function:
void doSomeStuffWithTheString(const std::string& value) {
...
std::string v = value;
std::cout << value.c_str();
...
}
but then i call this with
doSomeStuffWithTheString("foo");
and it works. So i would have thought that…

lurscher
- 25,930
- 29
- 122
- 185
5
votes
2 answers
compiler cares about copy constructor when it doesn't need one
Why compiler cares about copy constructor when it doesn't need one?
#include
template
void print(T);
class Foo {
Foo(const Foo&);
public:
Foo(){}
};
template<>
void print(const Foo& f) {
std::cout <<…

Jouni
- 321
- 1
- 8
5
votes
1 answer
shared_ptr to const shared_ptr&
I feel confused about shared_ptr, and my main question is: does c++ create a new object (shared_ptr object) when I do the following?
void Func1(const shared_ptr& rhs) {}
void Func2(const shared_ptr& rhs) {}
shared_ptr…

JJ.
- 111
- 11
5
votes
3 answers
What is the difference between temporary variable and constant in C++?
Allow me to post my code first:
void Fun(short &s){}
void FunCon(const short &s){}
int main()
{
int iTest = 20;//note: iTest is int but parameter of Fun is "short &"
Fun(iTest);//error, I know there is a temp variable(typecast)
…

Joe
- 410
- 3
- 13
5
votes
1 answer
Pass by const reference in C
Does C support pass by const reference like C++? If not, are there other ways to make pass-by-value more efficient? I don't think it makes sense to pass references to a function only because it's more efficient.

Milad
- 4,901
- 5
- 32
- 43