pass-by-value

How to pass pointer to function and dynamically allocate memory within function C++

£可爱£侵袭症+ 提交于 2020-12-05 07:22:20
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

北慕城南 提交于 2020-11-26 13:28:31
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

蹲街弑〆低调 提交于 2020-11-26 13:21:52
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

天涯浪子 提交于 2020-11-26 13:20:40
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Canonical implementation of operator+ involves additional move constructor

半腔热情 提交于 2020-06-11 06:09:08
问题 Motivated by this question, I compared two different versions of an implementation of a binary operator+ in terms of operator+= . Consider we are inside the definition of class X . Version 1 friend X operator+(X lhs, const X& rhs) { lhs += rhs; return lhs; } Version 2 friend X operator+(const X& lhs, const X& rhs) { X temp(lhs); temp += rhs; return temp; } friend X operator+(X&& lhs, const X& rhs) { lhs += rhs; return std::move(lhs); } Where, in both cases, operator+= is defined as follows: X

Why is this working? I cannot understand the logic of this swapping

主宰稳场 提交于 2020-05-16 03:22:17
问题 int main() { // Complete the program string a,b; getline(cin,a); getline(cin,b); cout<<a.size()<<" "; cout<<b.size(); string c=a+b; cout<<endl<<c; swap(a[0],b[0]); cout<<endl<<a<<" "<<b; return 0; } void swap(string s1,string s2){ string temp=s1; s1=s2; s2=temp; } Well the target is to swap the first element of both strings, but I created a general function for that and even got it right. But, unexpectedly, I didn't use pass by reference or pointer! Even then, the changes are permanent when I

Is RVO (Return Value Optimization) on unnamed objects a universally guaranteed behavior?

﹥>﹥吖頭↗ 提交于 2020-01-29 13:33:36
问题 This question is in different aspect (also limited to gcc). My question is meant only for unnamed objects . Return Value Optimization is allowed to change the observable behavior of the resulting program. This seems to be mentioned in standard also. However, this "allowed to" term is confusing. Does it mean that RVO is guaranteed to happen on every compiler. Due to RVO below code changes it's observable behavior: #include<iostream> int global = 0; struct A { A(int *p) {} A(const A &obj) { ++

Is RVO (Return Value Optimization) on unnamed objects a universally guaranteed behavior?

梦想与她 提交于 2020-01-29 13:32:55
问题 This question is in different aspect (also limited to gcc). My question is meant only for unnamed objects . Return Value Optimization is allowed to change the observable behavior of the resulting program. This seems to be mentioned in standard also. However, this "allowed to" term is confusing. Does it mean that RVO is guaranteed to happen on every compiler. Due to RVO below code changes it's observable behavior: #include<iostream> int global = 0; struct A { A(int *p) {} A(const A &obj) { ++

Can we overload a function based on only whether a parameter is a value or a reference?

时光怂恿深爱的人放手 提交于 2020-01-21 04:45:42
问题 I got the answer NO! Because passing by value and passing by reference looks identical to the caller. However, the code below compiles right class A { public: void f(int i) {} void f(int& i) {} }; But when I try to use it, there is compile error. int main () { A a; int i = 9; int& j = i; a.f(1); a.f(i); a.f(j); return 0; } Why does not the compiler disable it even without knowing it is going to be used? 回答1: Yes, they can be overloaded based on reference or not. That is why it's perfectly

Java: arrays as reference types, in methods

人盡茶涼 提交于 2020-01-15 11:38:36
问题 I am moving from C++ to Java, and I have a problem understanding how, in Java, an array lasts outside the method in which it was created. Take a look at this simple code below: public static int[] arrayMethod(){ int[] tempArray = {1, 2, 3, 4}; return tempArray; } public static void main(String[] args){ int arr[] = arrayMethod(); for(int i : arr){ System.out.println(i); } } In C++, unless the array is dynamically allocated with the new operator, the array would not exist after the call,