pass-by-reference

Are arrays in java pass by reference or pass by value?

倖福魔咒の 提交于 2021-02-04 19:39:07
问题 Are arrays in java pass by reference or pass by value? (I've similar questions asked before, but none of them seemed to get a good answer, so hopefully this time will be different). Suppose I have an array called data that contains Objects of some type. Now let us suppose that I pass and store that array in class A and then I pass it to class B and class B changes one of the entries of the array. Will class A's version of the array change? Does it matter if this was an array of primitives

std::optional<T> call by reference

旧街凉风 提交于 2021-01-29 07:11:36
问题 I am implementing the following function: bool DenStream::_try_merge(const double sample, const double weight, const std::optional<MicroCluster>& micro_cluster) const { if (micro_cluster) { MicroCluster micro_cluster_copy(*micro_cluster); micro_cluster_copy.insert_sample(sample, weight); if (micro_cluster_copy.get_radius() <= this->eps) { micro_cluster.insert_sample(sample, weight); // THIS DOES NOT WORK return true; } } return false; }; The compiler says error: 'const class std::optional'

Reference not changing the value of variable

房东的猫 提交于 2021-01-29 02:04:08
问题 I have written this program to find the first occurrence of the character in the user given string and frequency of that character. But when I print the value of variable i_r inside the main function it prints zero. But in side find_ch it shows right value. Why is this happening? #include<iostream> #include<string> using namespace std; string::size_type find_ch(string &str,char ch,int &i_r) { string::size_type first=0; for(auto i=static_cast<int>(str.size())-1;i>0;i--) { cout<<"Value of i_r :

Passing derived class to base function

[亡魂溺海] 提交于 2021-01-28 17:41:51
问题 I'm having trouble passing a derived class to a function which accepts the base class as argument. The base class is consists of "obstacles" which are to be placed on a "board" void Board::setvalue(int length, int width, Obstacle& obstacle); However, this causes the compiler to give the "no known conversion for argument..."-error. Reading up around the site i found that i should be passing the derived object as a const, this however causes problems because a const can't be assigned to the

Passing derived class to base function

时光怂恿深爱的人放手 提交于 2021-01-28 17:40:56
问题 I'm having trouble passing a derived class to a function which accepts the base class as argument. The base class is consists of "obstacles" which are to be placed on a "board" void Board::setvalue(int length, int width, Obstacle& obstacle); However, this causes the compiler to give the "no known conversion for argument..."-error. Reading up around the site i found that i should be passing the derived object as a const, this however causes problems because a const can't be assigned to the

How object passed as an argument ByVal should behave

主宰稳场 提交于 2021-01-28 14:19:37
问题 I am studying/learning the bahavior of ByVal and ByRef when it comed to working with a call object. So I created this class PersonModel Private Type TPerson firstName As String lastName As String End Type Private this As TPerson Public Property Get firstName() As String firstName = this.firstName End Property Public Property Let firstName(ByVal strNewValue As String) this.firstName = strNewValue End Property Public Property Get lastName() As String lastName = this.lastName End Property Public

How do I access 'this' from within a C# extension method?

自作多情 提交于 2021-01-28 08:57:08
问题 I've been working with Vector2's and XNA, and I've come to find that calling the Normalize() member function on a Zero Vector normalizes it to a vector of {NaN, NaN}. This is all well and good, but in my case I'd prefer it instead just leave them as Zero Vectors. Adding this code to my project enabled a cute extension method: using ExtensionMethods; namespace ExtensionMethods { public static class MyExtensions { public static Vector2 NormalizeOrZero(this Vector2 v2) { if (v2 != Vector2.Zero)

How do I access 'this' from within a C# extension method?

大城市里の小女人 提交于 2021-01-28 08:37:33
问题 I've been working with Vector2's and XNA, and I've come to find that calling the Normalize() member function on a Zero Vector normalizes it to a vector of {NaN, NaN}. This is all well and good, but in my case I'd prefer it instead just leave them as Zero Vectors. Adding this code to my project enabled a cute extension method: using ExtensionMethods; namespace ExtensionMethods { public static class MyExtensions { public static Vector2 NormalizeOrZero(this Vector2 v2) { if (v2 != Vector2.Zero)

Groovy (or Java) - Pass by reference into a wrapper object

偶尔善良 提交于 2021-01-28 06:21:01
问题 Is it possible in Java (or Groovy) to pass an object by reference into a wrapper object (ie: List or Map)? Example code (in Groovy): def object = null def map = [object: object] object = new Object() Unfortunately map.object remains null even though the object variable doesn't, so obviously the original creation of the map was done by value not by reference. Is it possible to create a List or Map via references so that when the object outside the List or Map changes the change is reflected

Force C++ to assign new addresses to arguments

≡放荡痞女 提交于 2021-01-28 06:10:43
问题 It seems that when I pass different integers directly to a function, C++ assigns them the same address as opposed to assigning different addresses to different values. Is this by design, or an optimization that can be turned off? See the code below for an illustration. #include <iostream> const int *funct(const int &x) { return &x; } int main() { int a = 3, b = 4; // different addresses std::cout << funct(a) << std::endl; std::cout << funct(b) << std::endl; // same address std::cout << funct