pass-by-value

Visitor class holding large shared state: best way to implement reference semantics?

依然范特西╮ 提交于 2019-12-10 16:15:31
问题 This question is loosely based on the Boost.Graph library (BGL) that uses a Visitor-like pattern to customize recursive (search) algorithms. The BGL passes visitor objects by value (in analogy with the STL function objects) and the documentation states Since the visitor parameter is passed by value, if your visitor contains state then any changes to the state during the algorithm will be made to a copy of the visitor object, not the visitor object passed in. Therefore you may want the visitor

Can I have a function/method passing an argument by reference and an overload passing it by value in C++? [duplicate]

泄露秘密 提交于 2019-12-10 13:48:16
问题 This question already has answers here : Function Overloading Based on Value vs. Const Reference (5 answers) Closed 6 years ago . In C# this is certainly possible, as this compilable example can show: static void Teste(int x) { } static void Teste(ref int x) { } static void Teste() { int i = 0; Teste(i); Teste(ref i); } But can it be done in C++(/CLI) with a constructor? See the example below: class Foo { Foo(int bar) { // initializing "Foo" instance... } Foo(int &bar) { // initializing "Foo"

Will any compiler actually ever elide these copies?

一曲冷凌霜 提交于 2019-12-10 13:37:19
问题 Given struct Range{ Range(double from, double to) : from(from), to(to) {} double from; double to; // if it matters to the compiler, we can add more fields here to make copying expensive }; struct Box{ Box(Range x, Range y) : x(x), y(y) {} Range x; Range y; }; someone said that in Box box(Range(0.0,1.0),Range(0.0,2.0)) , the compiler can avoid copying Range objects altogether by constructing them inside box to begin with. Does any compiler actually do this? My own attempts haven't succeeded.

Passing inline double array as method argument

我怕爱的太早我们不能终老 提交于 2019-12-10 12:38:06
问题 Consider method functionA (double[] arg) I want to pass a double array inline, like functionA({1.9,2.8}) and not create an array first and then pass it, like double var[] = {1.0,2.0}; functionA(var); Is this possible with C++? Sounds simple, but I could not find a hint anyway concerning my question which made me suspicious :). 回答1: You can do this with std::initializer_list<> #include<vector> void foo(const std::initializer_list<double>& d) { } int main() { foo({1.0, 2.0}); return 0; } Which

Is it passing by pointer?

自闭症网瘾萝莉.ら 提交于 2019-12-10 10:25:02
问题 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? 回答1: 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

Passing to a Reference Argument by Value

徘徊边缘 提交于 2019-12-09 03:29:16
问题 Consider this simple program: vector<int> foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo), foo.front(), 13); for(const auto& i : foo) cout << i << '\t'; When I wrote it I expected to get: 13 42 13 42 13 42 But instead I got: 13 42 0 42 0 42 The problem of course is that replace takes in the last 2 parameters by reference. So if either of them happen to be in the range being operated on the results may be unexpected. I can solve this by adding a temporary variable: vector<int> foo =

C++: How do I decide if to pass params by ref or by value?

大兔子大兔子 提交于 2019-12-09 02:02:27
问题 With C++ how do i decide if i should pass an argument by value or by reference/pointer? (tell me the answer for both 32 and 64bits) Lets take A. Is 2 32bit values more less or equal work as a pointer to a 32bit value? B to me seems like i always should pass by value. C i think i should pass by value but someone told me (however i haven't seen proof) that processors don't handle values not their bitsize and so it is more work. So if i were passing them around would it be more work to pass by

Confused by single pointer and double pointer arguments in function calls

蹲街弑〆低调 提交于 2019-12-08 22:59:55
问题 I'm trying to get a deeper understanding on pointer arguments in functions for C. I've written a test program to try to see the difference between passing a single pointer vs a double pointer to a function and then modifying it. I have a program that has two functions. The first function modifyMe1 takes a single pointer as an argument and changes the a property to 7. The second function modifyMe2 takes a double pointer as an argument and changes the a property to 7. I expected that the first

How to pass by reference in Ruby?

妖精的绣舞 提交于 2019-12-08 13:56:05
问题 Currently I'm developing a Watcher class in Ruby, which, among other things, is able to find the period duration of a toggling signal. This is in general rather simple, but a problem I'm facing is that apparently Ruby passes all parameters by value. While researching online I found many different discussions about what "pass by value" and "pass by reference" actually is, but no actual "how to". Coming from a C/C++ background, for me this is an essential part of a programming/scripting

Properties - by value or reference?

隐身守侯 提交于 2019-12-07 00:07:40
问题 I've got the following public property which exposes an Arraylist : public ArrayList SpillageRiskDescriptions { get { return _SpillageRiskDescriptions; } set { _SpillageRiskDescriptions = value; } } Elsewhere I'm calling SpillageRiskDescriptions.Add("VENTILATE AREA"); SpillageRiskDescriptions.Add("DO NOT ALLOW SPILLAGE TO ENTER MAINS"); These seem to be adding elements to the private ArrayList _SpillageRiskDescriptions (through the property) whereas I would've expected this to cause a problem