return-by-reference

Scheme pass-by-reference

血红的双手。 提交于 2021-02-17 21:51:10
问题 How can I pass a variable by reference in scheme? An example of the functionality I want: (define foo (lambda (&x) (set! x 5))) (define y 2) (foo y) (display y) ;outputs: 5 Also, is there a way to return by reference? 回答1: See http://community.schemewiki.org/?scheme-faq-language question "Is there a way to emulate call-by-reference?". In general I think that fights against scheme's functional nature so probably there is a better way to structure the program to make it more scheme-like. 回答2:

Overloaded [] operator on template class in C++ with const / nonconst versions

自作多情 提交于 2020-08-27 01:30:30
问题 Whew, that was a long title. Here's my problem. I've got a template class in C++ and I'm overloading the [] operator. I have both a const and a non-const version, with the non-const version returning by reference so that items in the class can be changed as so: myobject[1] = myvalue; This all works until I use a boolean as the template parameter. Here's a full example that shows the error: #include <string> #include <vector> using namespace std; template <class T> class MyClass { private:

Overloaded [] operator on template class in C++ with const / nonconst versions

断了今生、忘了曾经 提交于 2020-08-27 01:30:10
问题 Whew, that was a long title. Here's my problem. I've got a template class in C++ and I'm overloading the [] operator. I have both a const and a non-const version, with the non-const version returning by reference so that items in the class can be changed as so: myobject[1] = myvalue; This all works until I use a boolean as the template parameter. Here's a full example that shows the error: #include <string> #include <vector> using namespace std; template <class T> class MyClass { private:

How to return references to object created inside a method

我的梦境 提交于 2020-01-13 10:35:07
问题 I am reasoning about the best approach to return references to objects created inside a method, like in the following situation: class A{ public: A(){} ~A(){} }; class Foo{ public: Foo(){} ~Foo(){} A& create(int random_arg){ // create object A and return its reference } }; void other_method(){ Foo f; A a = f.create(); // do stuff with a { I have considered three possible solutions: create a raw pointer and return a reference, but this is bad because there is no guarantee that the object will

C++ Return value, reference, const reference

帅比萌擦擦* 提交于 2019-12-31 08:25:03
问题 Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the

C++ Return value, reference, const reference

一个人想着一个人 提交于 2019-12-31 08:24:49
问题 Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the

Optimal way to return local value in C++11

人盡茶涼 提交于 2019-12-22 15:25:47
问题 In the old days, if I wanted a string representation of an object A , I would write something with the signature void to_string(const A& a, string& out) to avoid extra copies. Is this still the best practice in C++11, with move semantics and all? I have read several comments on other contexts that suggest relying on RVO and instead writing string to_string(const A& a) . But RVO is not guaranteed to happen! So, how can I, as the programmer of to_string, guarantee the string is not copied

Optimal way to return local value in C++11

流过昼夜 提交于 2019-12-22 15:25:14
问题 In the old days, if I wanted a string representation of an object A , I would write something with the signature void to_string(const A& a, string& out) to avoid extra copies. Is this still the best practice in C++11, with move semantics and all? I have read several comments on other contexts that suggest relying on RVO and instead writing string to_string(const A& a) . But RVO is not guaranteed to happen! So, how can I, as the programmer of to_string, guarantee the string is not copied

Why is returning a reference to a function local value not a compile error?

徘徊边缘 提交于 2019-12-18 03:51:27
问题 The following code invokes undefined behaviour. int& foo() { int bar = 1234; return bar; } g++ issues a warning: warning: reference to local variable ‘bar’ returned [-Wreturn-local-addr] clang++ too: warning: reference to stack memory associated with local variable 'bar' returned [-Wreturn-stack-address] Why is this not a compile error (ignoring -Werror )? Is there a case where returning a ref to a local var is valid? EDIT As pointed out, the spec mandates this be compilable. So, why does the

PHP variables: references or copies

早过忘川 提交于 2019-12-11 06:49:14
问题 I'm confused about how PHP variable references work. In the examples below, I want to be able to access the string hello either as $bar[0] or $barstack[0][0] . It would seem that passing the array by reference in step 1 should be sufficient. The second example does not work. $foostack[0]0] is the string hello , but $foo[0] doesn't exist. At some point, the first element of $foostack becomes a copy of $foo, instead of a reference. The problem lies in the first line of step 2: When I push a