pass-by-value

Modify elements of vector (by value, by reference) Function C++

孤人 提交于 2019-12-06 17:25:45
问题 I have a function where I have to modifiy the values of a vector. is it a good practice in C++ to return the vector? Function 1: vector<string> RemoveSpecialCharacters(vector<string> words) { for (vector<string>::iterator it=words.begin(); it!=words.end(); ) { if(CheckLength(*it) == false) { it = words.erase(it); } else{ ++it; } }//end for return words; } Function 2: void RemoveSpecialCharacters(vector<string> & words) { for (vector<string>::iterator it=words.begin(); it!=words.end(); ) { if

C# Object property modified in the called function affect caller value

江枫思渺然 提交于 2019-12-06 15:11:54
问题 I defined a simple class with the following definition. In this sample, I defined a simple class with a single string parameter. Then I instantiated this class and assigned a value to the parameter. I passed that instance to another function without specifying "ref" keyword. It means it should be passed by value instead of reference. But what I don't understand is the reason that the output displays "Second Modification" value as a result instead of "First Modification". UPDATE 1: I think my

Garbage collection - why is c3 not eligible for collection in this example (SCJP 6) [duplicate]

旧时模样 提交于 2019-12-06 06:37:01
问题 This question already has answers here : Objects eligible for garbage collection (5 answers) Closed 6 years ago . Taken from SCJP 6 prep book - Given: class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } } When // doStuff is reached, how many objects are eligible for GC? A. 0 B. 1 C. 2 D. Compilation

Cost of passing by reference when a template type is fundamental

旧巷老猫 提交于 2019-12-06 05:06:15
问题 I've always heard that a good practice to ensure best performance was to: pass fundamental types ( int , double ...) by value pass classes by const reference Nowadays, using C++11 and full optimizations under a compiler, is there an overhead when one passes a fundamental type by const reference? And furthermore, when T is int will the following function: template <typename T> inline void f(const T& x); be slower than: template <typename T> inline void f(const T x); 回答1: If the compiler is

Is it passing by pointer?

旧巷老猫 提交于 2019-12-06 03:22:25
void func(char* buf) { buf++;} Should I call it passing by pointer or just passing by value(with the value being pointer type)? Would the original pointer passed in be altered in this case? This is passing by value. void func( char * b ) { b = new char[4]; } int main() { char* buf = 0; func( buf ); delete buf; return 0; } buf will still be 0 after the call to func and the newed memory will leak. When you pass a pointer by value you can alter what the pointer points to not the pointer itself. The right way to do the above stuff would be ALTERNATIVE 1 void func( char *& b ) { b = new char[4]; }

Operator overloading by value results in use of moved value

ぐ巨炮叔叔 提交于 2019-12-05 06:56:44
Compiling the following Rust code that uses operator overloading use std::ops::{Add}; #[derive(Show)] struct Point { x: int, y: int } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point {x: self.x + other.x, y: self.y + other.y} } } fn main() { let p: Point = Point {x: 1, y: 0}; let pp = p + p; } Results in compiler errors due to ownership of p: <anon>:21:18: 21:19 error: use of moved value: `p` <anon>:21 let pp = p + p; ^ <anon>:21:14: 21:15 note: `p` moved here because it has type `Point`, which is non-copyable <anon>:21 let pp = p + p; ^ The rationale

Pass-by-value (StringBuilder vs String) [duplicate]

三世轮回 提交于 2019-12-05 00:37:32
This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 86 answers I do not understand why System.out.println(name) outputs Sam without being affected by the method's concat function, while System.out.println(names) outputs Sam4 as a result of the method's append method. Why is StringBuilder affected and not String? Normally, calling methods on a reference to an object affects the caller, so I do not understand why the String result remains unchanged. Thanks in advance public static String speak(String name) { name = name.concat("4"); return name; } public

Modify elements of vector (by value, by reference) Function C++

为君一笑 提交于 2019-12-04 23:03:11
I have a function where I have to modifiy the values of a vector. is it a good practice in C++ to return the vector? Function 1: vector<string> RemoveSpecialCharacters(vector<string> words) { for (vector<string>::iterator it=words.begin(); it!=words.end(); ) { if(CheckLength(*it) == false) { it = words.erase(it); } else{ ++it; } }//end for return words; } Function 2: void RemoveSpecialCharacters(vector<string> & words) { for (vector<string>::iterator it=words.begin(); it!=words.end(); ) { if(CheckLength(*it) == false) { it = words.erase(it); } else{ ++it; } }//end for } billz Your two

Trying to populate an array using a function in C++ [closed]

痴心易碎 提交于 2019-12-04 21:40:43
So, I'm trying to populate and print a small array using functions, however I've hit a bit of a roadblock. The code I have is: #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; struct planes { string name; int seats; int range; int weight; }; int populate (planes planesArray[5]) { string name; int seats, range, weight, i; ifstream infile; infile.open("plane_data.txt"); if (!infile) { cout << "File cannot be reached"; } for (int i = 0; i < 4; i++){ infile >> name; infile >> seats; infile >> range; infile >> weight; } infile.close(); } int main() {

C# Object property modified in the called function affect caller value

。_饼干妹妹 提交于 2019-12-04 20:55:36
I defined a simple class with the following definition. In this sample, I defined a simple class with a single string parameter. Then I instantiated this class and assigned a value to the parameter. I passed that instance to another function without specifying "ref" keyword. It means it should be passed by value instead of reference. But what I don't understand is the reason that the output displays "Second Modification" value as a result instead of "First Modification". UPDATE 1: I think my question was confusing. I know how "ref" and "pass by reference" working. I need to know why when I