pass-by-reference

How do you force compiler to pass some variable by reference in C++?

末鹿安然 提交于 2020-01-01 11:55:28
问题 Here is a simple example; template <typename T> void foo(T t) {} std::string str("some huge text"); foo(str); My question is how can I force the compiler to pass str by reference without modifying function foo? 回答1: Pass the reference type explicitly: template <typename T> void foo(T t) {} int main() { std::string str("some huge text"); foo<std::string&>(str); } This does modify the function instantiation that you get (by generating a void foo<std::string&>(std::string& t) ), but it doesn't

PHP Pass by reference confusion

大兔子大兔子 提交于 2020-01-01 11:05:33
问题 To cut a long story short I have the following function as part of my framework: public function use_parameters() { $parameters = func_get_args(); $stack = debug_backtrace(); foreach($stack[0]['args'] as $key => &$parameter) { $parameter = array_shift($this->parameter_values); } } Where $this->parameter_values = array('value1', 'value2', 'value3', 'value4', 'value5', ...); Which is used in the following context: $instance->use_parameters(&$foo, &$bah); Assigning: $foo = 'value1'; $bah =

PHP Pass by reference confusion

杀马特。学长 韩版系。学妹 提交于 2020-01-01 11:04:03
问题 To cut a long story short I have the following function as part of my framework: public function use_parameters() { $parameters = func_get_args(); $stack = debug_backtrace(); foreach($stack[0]['args'] as $key => &$parameter) { $parameter = array_shift($this->parameter_values); } } Where $this->parameter_values = array('value1', 'value2', 'value3', 'value4', 'value5', ...); Which is used in the following context: $instance->use_parameters(&$foo, &$bah); Assigning: $foo = 'value1'; $bah =

PHP Pass by reference confusion

怎甘沉沦 提交于 2020-01-01 11:04:00
问题 To cut a long story short I have the following function as part of my framework: public function use_parameters() { $parameters = func_get_args(); $stack = debug_backtrace(); foreach($stack[0]['args'] as $key => &$parameter) { $parameter = array_shift($this->parameter_values); } } Where $this->parameter_values = array('value1', 'value2', 'value3', 'value4', 'value5', ...); Which is used in the following context: $instance->use_parameters(&$foo, &$bah); Assigning: $foo = 'value1'; $bah =

Pass a reference to a reference

纵饮孤独 提交于 2020-01-01 09:35:12
问题 I think it's illegal to pass a reference to a reference in C++.However ,when I run this code it gives me no error. void g(int& y) { std::cout << y; y++; } void f(int& x) { g(x); } int main() { int a = 34; f(a); return 0; } Doesn't the formal parameter of g() qualify as a reference to a reference ?? 回答1: 1) There is nothing wrong with passing a reference to a reference (it is what the move-constructor and move-assignment operators use - though it is actually called a rvalue-reference). 2) What

Pretending .NET strings are value type

﹥>﹥吖頭↗ 提交于 2020-01-01 03:12:48
问题 In .NET, strings are immutable and are reference type variables. This often comes as a surprise to newer .NET developers who may mistake them for value type objects due to their behavior. However, other than the practice of using StringBuilder for long concatenation esp. in loops, is there any reason in practice that one needs to know this distinction? What real-world scenarios are helped or avoided by understanding the value-reference distinction with regard to .NET strings vs. just

Pretending .NET strings are value type

不想你离开。 提交于 2020-01-01 03:11:10
问题 In .NET, strings are immutable and are reference type variables. This often comes as a surprise to newer .NET developers who may mistake them for value type objects due to their behavior. However, other than the practice of using StringBuilder for long concatenation esp. in loops, is there any reason in practice that one needs to know this distinction? What real-world scenarios are helped or avoided by understanding the value-reference distinction with regard to .NET strings vs. just

Intuitive understanding of functions taking references of references [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-31 09:03:14
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding my intuition, and I cannot find any explanation on the internet. What does it mean for a C++ function to take a reference of a reference? For example: void myFunction(int&& val); //what does this mean?! I understand the idea of passing-by-reference, so void addTwo(int& a) { a += 2; } int main() { int x = 5; addTwo(x); return 0; } works and is

Pass by value faster than pass by reference

大憨熊 提交于 2019-12-31 07:59:07
问题 I made a simple program in c++ to compare performance between two approaches - pass by value and pass by reference. Actually pass by value performed better than pass by reference. The conclusion should be that passing by value require fewer clock-cycles (instructions) I would be really glad if someone could explain in detail why pass by value require fewer clock-cycles. #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; void function(int *ptr); void function2(int

Modifying string literal passed in as a function

时间秒杀一切 提交于 2019-12-31 05:29:06
问题 If I have a function in program int main(){ char *name = "New Holland"; modify(name); printf("%s\n",name); } that calls this function void modify(char *s){ char new_name[10] = "Australia"; s = new_name; /* How do I correct this? */ } how can I update the value of the string literal name (which now equals new Holland) with Australia. The problem I think that I face is the new_name is local storage, so after the function returns, the variable is not stored 回答1: Try this: #include <stdio.h> void