function-parameter

assignment operator within function parameter C++

人走茶凉 提交于 2019-11-30 09:07:31
I'm studying data structures (List, Stack, Queue), and this part of code is confusing me. ListNode( const Object& theElement = Object(), ListNode * node = NULL); template<class Object> ListNode<Object>::ListNode( const Object& theElement, ListNode<Object> * node) { element = theElement; next = node; } Why there are assignment operators within function parameters? What does Object() call do? Charles Salvia Those are not assignment operators. Those are default arguments for the function. A function can have one or more default arguments , meaning that if, at the calling point, no argument is

Using function arguments as local variables

蓝咒 提交于 2019-11-30 06:48:39
Something like this (yes, this doesn't deal with some edge cases - that's not the point): int CountDigits(int num) { int count = 1; while (num >= 10) { count++; num /= 10; } return count; } What's your opinion about this? That is, using function arguments as local variables. Both are placed on the stack, and pretty much identical performance wise, I'm wondering about the best-practices aspects of this. I feel like an idiot when I add an additional and quite redundant line to that function consisting of int numCopy = num , however it does bug me. What do you think? Should this be avoided? As a

c++0x: proper way to receive a lambda as parameter by reference

你离开我真会死。 提交于 2019-11-29 19:56:18
What is the right way to define a function that receives a int->int lambda parameter by reference? void f(std::function< int(int) >& lambda); or void f(auto& lambda); I'm not sure the last form is even legal syntax. Are there other ways to define a lambda parameter? You cannot have an auto parameter. You basically have two options: Option #1: Use std::function as you have shown. Option #2: Use a template parameter: template<typename F> void f(F &lambda) { /* ... */} Option #2 may, in some cases, be more efficient, as it can avoid a potential heap allocation for the embedded lambda function

Why should I declare a C array parameter's size in a function header?

妖精的绣舞 提交于 2019-11-29 11:21:49
问题 Can anyone enlighten me as to why I should bother to specify the size of a C array argument in a function header? For example: void foo (int iz[6]) { iz[42] = 43; } With: int is[2] = {1,2,3}; we get a useful error. Perhaps it helps with commenting/documentation? 回答1: Can anyone enlighten me as to why I should bother to specify the size of a C array argument in a function header? For example: void foo (const char sz[6]) { sz[42] = 43; } IMO, you shouldn't. When you try to pass an array to a

Will a reference bound to a function parameter prolong the lifetime of that temporary?

白昼怎懂夜的黑 提交于 2019-11-29 06:39:20
I have this code (simplified version): const int& function( const int& param ) { return param; } const int& reference = function( 10 ); //use reference I can't quite decide to which extent C++03 Standard $12.2/5 wording The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference... is applicable here. Is reference variable in the code above valid or dangling? Will the reference in the calling code prolong the lifetime of the temporary passed as the parameter? Daniel Trebbien

String in function parameter

早过忘川 提交于 2019-11-28 23:41:20
int main() { char *x = "HelloWorld"; char y[] = "HelloWorld"; x[0] = 'Z'; //y[0] = 'M'; return 0; } In the above program, HelloWorld will be in read-only section(i.e string table). x will be pointing to that read-only section, so trying to modify that values will be undefined behavior. But y will be allocated in stack and HelloWorld will be copied to that memory. so modifying y will works fine. String literals: pointer vs. char array Here is my Question: In the following program, both char *arr and char arr[] causes segmentation fault if the content is modified. void function(char arr[]) /

Assign function arguments to `self`

你离开我真会死。 提交于 2019-11-28 22:55:19
I've noticed that a common pattern I use is to assign SomeClass.__init__() arguments to self attributes of the same name. Example: class SomeClass(): def __init__(self, a, b, c): self.a = a self.b = b self.c = c In fact it must be a common task for others as well as PyDev has a shortcut for this - if you place the cursor on the parameter list and click Ctrl+1 you're given the option to Assign parameters to attributes which will create that boilerplate code for you. Is there a different, short and elegant way to perform this assignment? I sympathize with your sense that boilerplate code is a

c++0x: proper way to receive a lambda as parameter by reference

本秂侑毒 提交于 2019-11-28 16:16:39
问题 What is the right way to define a function that receives a int->int lambda parameter by reference? void f(std::function< int(int) >& lambda); or void f(auto& lambda); I'm not sure the last form is even legal syntax. Are there other ways to define a lambda parameter? 回答1: You cannot have an auto parameter. You basically have two options: Option #1: Use std::function as you have shown. Option #2: Use a template parameter: template<typename F> void f(F &lambda) { /* ... */} Option #2 may, in

Will a reference bound to a function parameter prolong the lifetime of that temporary?

孤街浪徒 提交于 2019-11-28 00:29:41
问题 I have this code (simplified version): const int& function( const int& param ) { return param; } const int& reference = function( 10 ); //use reference I can't quite decide to which extent C++03 Standard $12.2/5 wording The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference... is applicable here. Is reference variable in the code above valid or dangling? Will the

PHP: variable-length argument list by reference?

旧时模样 提交于 2019-11-27 22:45:19
Is it possible to create a PHP function that takes a variable number of parameters all of them by reference? It doesn't help me a function that receives by reference an array of values nor a function that takes its arguments wrapped in an object because I'm working on function composition and argument binding. Don't think about call-time pass-by-reference either. That thing shouldn't even exist. PHP 5.6 introduced new variadic syntax which supports pass-by-reference. (thanks @outis for the update) function foo(&...$args) { $args[0] = 'bar'; } For PHP 5.5 or lower you can use the following