assignment-operator

What's the use of the private copy constructor in c++

你离开我真会死。 提交于 2019-11-26 10:31:33
问题 Why do people define a private copy constructor? When is making the copy constructor and the assignment operator private a good design? If there are no members in the class which are pointers or handles to a unique object (like file name), then wat other cases are there where private copy constructor is a good idea? Same question apply for assignment operator. Given that majority of C++ revolves around copying of objects and passing by reference, are there any good designs which involve

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?

霸气de小男生 提交于 2019-11-26 10:25:33
What is the motivation for Scala assignment evaluating to Unit rather than the value assigned? A common pattern in I/O programming is to do things like this: while ((bytesRead = in.read(buffer)) != -1) { ... But this is not possible in Scala because... bytesRead = in.read(buffer) .. returns Unit, not the new value of bytesRead. Seems like an interesting thing to leave out of a functional language. I am wondering why it was done so? I advocated for having assignments return the value assigned rather than unit. Martin and I went back and forth on it, but his argument was that putting a value on

Why should the assignment operator return a reference to the object?

妖精的绣舞 提交于 2019-11-26 09:09:54
问题 I\'m doing some revision of my C++, and I\'m dealing with operator overloading at the minute, specifically the \"=\"(assignment) operator. I was looking online and came across multiple topics discussing it. In my own notes, I have all my examples taken down as something like class Foo { public: int x; int y; void operator=(const Foo&); }; void Foo::operator=(const Foo &rhs) { x = rhs.x; y = rhs.y; } In all the references I found online, I noticed that the operator returns a reference to the

Why cannot a non-member function be used for overloading the assignment operator?

爷,独闯天下 提交于 2019-11-26 07:40:48
问题 The assignment operator can be overloaded using a member function but not a non-member friend function: class Test { int a; public: Test(int x) :a(x) {} friend Test& operator=(Test &obj1, Test &obj2); }; Test& operator=(Test &obj1, Test &obj2)//Not implemented fully. just for test. { return obj1; } It causes this error: error C2801: \'operator =\' must be a non-static member Why cannot a friend function be used for overloading the assignment operator? The compiler is allowing to overload

How are C++ array members handled in copy control functions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 07:34:52
问题 This is something I have wondered for a long time. Take the following example: struct matrix { float data[16]; }; I know what the default constructor and destructor do in this specific example (nothing), but what about the copy constructor and the copy assignment operator? struct matrix { float data[16]; // automatically generated copy constructor matrix(const matrix& that) : // What happens here? { // (or here?) } // automatically generated copy assignment operator matrix& operator=(const

The copy constructor and assignment operator

…衆ロ難τιáo~ 提交于 2019-11-26 07:28:12
问题 If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically \'inherit\' the behavior from the copy constructor? 回答1: No, they are different operators. The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by

Scalar vs List Assignment Operator

点点圈 提交于 2019-11-26 06:48:11
问题 Please help me understand the following snippets: my $count = @array; my @copy = @array; my ($first) = @array; (my $copy = $str) =~ s/\\\\/\\\\\\\\/g; my ($x) = f() or die; my $count = () = f(); print($x = $y); print(@x = @y); 回答1: The symbol = is compiled into one of two assignment operators: A list assignment operator ( aassign ) is used if the left-hand side (LHS) of a = is some kind of aggregate. A scalar assignment operator ( sassign ) is used otherwise. The following are considered to

Reference assignment operator in PHP, =&

六月ゝ 毕业季﹏ 提交于 2019-11-26 05:58:20
问题 What does the =& (equals-ampersand) assignment operator do in PHP? Is it deprecated? 回答1: It's not deprecated and is unlikely to be. It's the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data. It's called assignment by reference , which, to quote the manual, "means that both variables end up pointing at the same data, and nothing is copied anywhere". The only thing that is deprecated with =& is "assigning the

When is overloading pass by reference (l-value and r-value) preferred to pass-by-value?

自古美人都是妖i 提交于 2019-11-26 05:27:20
问题 I have seen it said that a operator= written to take a parameter of the same type by-value serves as both copy assignment operator and move assignment operator in C++11: Foo& operator=(Foo f) { swap(f); return *this; } Where the alternative would be more than twice as many lines with a lot of code repetition, and potential for error: Foo& operator=(const Foo& f) { Foo f2(f); swap(f2); return *this; } Foo& operator=(Foo&& f) { Foo f2(std::move(f)); swap(f2); return *this; } In what

Assign multiple objects to .GlobalEnv from within a function

纵饮孤独 提交于 2019-11-26 04:50:29
问题 A post on here a day back has me wondering how to assign values to multiple objects in the global environment from within a function. This is my attempt using lapply ( assign may be safer than <<- but I have never actually used it and am not familiar with it). #fake data set df <- data.frame( x.2=rnorm(25), y.2=rnorm(25), g=rep(factor(LETTERS[1:5]), 5) ) #split it into a list of data frames LIST <- split(df, df$g) #pre-allot 5 objects in R with class data.frame() V <- W <- X <- Y <- Z <- data