Questions tagged [pass-by-const-reference]

52 questions
0
votes
2 answers

Should compiler warn about unsafe behaviour when using temporary object on const ref type parameters?

Let's consider following improper code: #include #include struct C { C(const std::string &_s): s(_s) { } void run() { std::cout << "Here we are using the string " << s << "\n"; } const std::string &s; }; int…
ArturFH
  • 1,697
  • 15
  • 28
0
votes
3 answers

Argument passed by value or const reference in the constructor

Which constructor is better for the following class? struct Foo { Foo(const int& val):val_(val){} // constructor 1 Foo(int val):val_(val){} // constructor 2 int val_; }; Without any compiler optimization, do they copy val only once or…
0
votes
2 answers

Passing a struct of dynamic arrays by const reference

If I have a struct in C++ containing dynamically allocated arrays, for example: typedef struct foo { int* p; } foo; foo aFoo; aFoo.p = new int[n]; for(int i = 0; i < n; ++i) aFoo.p[i] = 0; How can I pass it by reference to a function so that…
0
votes
1 answer

Understanding call by value-result and call by reference differnce

So I have this piece of Pascal code: program P; var a: array [1..2] of Integer; var i :Integer; var k :Integer; procedure update(x,y,z: Integer); begin x := x+1; y := x*2; x := y; k := x; end begin …
0
votes
2 answers

C++ pass-by-non-const-reference method inside pass-by-const-reference method

I have this function pass_by_const(const std::string& s) which wants to call pass_by_non_const(std::string& s). If I have this method's definition pass_by_const(const std::string& s) { pass_by_non_const(s); } would compiler yield at me, and…
0
votes
2 answers

Transmit parameter by value or by reference in C++?

Possible Duplicate: Pass by reference more expensive than pass by value I want to know which is better, sending parameters by value or by reference in C++. I heard that there are cases where sending by value is faster than sending by reference.…
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
-1
votes
2 answers

Is there any realistic use case for passing pointers by constant reference rather than by value?

Since a reference (some-type&) behaves as a constant pointer (some-type * const), passing an argument via a "constant reference to pointer" parameter (some-type * const&) seems a bit redundant, because if the parameter is some-type * I'm copying…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1 2 3
4