pass-by-value

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

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

Pass int by const reference or by value , any difference? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-30 09:10:20
问题 This question already has answers here : Is it counter-productive to pass primitive types by reference? [duplicate] (7 answers) Closed 4 years ago . When I pass primitives like int and double to functions , is it better to pass them by const reference , or by value (assuming that I don't change the variable's value) ? int getValueFromArray(int index) { // return the value from the array } int getValueFromArray(const int& index) { // return the value from the array } Thanks 回答1: For primitive

Pass int by const reference or by value , any difference? [duplicate]

我只是一个虾纸丫 提交于 2019-12-30 09:10:09
问题 This question already has answers here : Is it counter-productive to pass primitive types by reference? [duplicate] (7 answers) Closed 4 years ago . When I pass primitives like int and double to functions , is it better to pass them by const reference , or by value (assuming that I don't change the variable's value) ? int getValueFromArray(int index) { // return the value from the array } int getValueFromArray(const int& index) { // return the value from the array } Thanks 回答1: For primitive

Variable changed in function not seen by caller?

 ̄綄美尐妖づ 提交于 2019-12-29 01:47:09
问题 Yes, I know it sounds silly but i have no idea what i'm doing wrong! The function is part of a poker game, in which there are 10 functions, each which checks for a specific poker hand. If activated, the function prints the line "Player 1 has a full house!" or whatever the hand might be. But, i also need to increment the value of p1, in which p1 is a global variable the holds the total score for p1. Printing the line works perfectly, but when i want to assign, for example, a value of 10 to p1,

Pass by value or reference, to a C++ constructor that needs to store a copy?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 01:37:14
问题 Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or reference-to-const, when it needs to store a copy of the argument(s) in its object either way? Here is the shortest example I can think of: struct foo { bar _b; foo(bar [const&] b) // pass by value or reference-to-const? : _b(b) { } }; The idea here is that I want to minimize the calls to bar's copy constructor when a foo object is created, in any of the various ways in which a foo object might get

Pass list to function by value

不问归期 提交于 2019-12-28 06:49:09
问题 I want to pass a list into function by value. By default, lists and other complex objects passed to function by reference. Here is some desision: def add_at_rank(ad, rank): result_ = copy.copy(ad) .. do something with result_ return result_ Can this be written shorter? In other words, I wanna not to change ad . 回答1: You can use [:] , but for list containing lists(or other mutable objects) you should go for copy.deepcopy() : lis[:] is equivalent to list(lis) or copy.copy(lis) , and returns a

why copy constructor is call when we pass an object as an argument by value to a method

爱⌒轻易说出口 提交于 2019-12-28 06:30:31
问题 i am new to C++ programming, when i am doing some C++ programs i have got a doubt that is why copy constructor is called when i pass an object as argument by value to a function. please see my below code in that i am passing a object of class as an argument by value to a function display() but it calling copy constructor and then control is hitting the display() function but i am understanding why it so please help. #include "stdafx.h" #include <iostream> using namespace std; class ClassA {