assignment-operator

Checklist for writing copy constuctor and assignment operator in C++

Please write a list of tasks that a copy constructor and assignment operator need to do in C++ to keep exception safety, avoid memory leaks etc. ...

How to use base class's constructors and assignment operator in C++?

I have class B with a set of constructors and an assignment operator. class B { public: B(); B(const string & s); B(const B & b){(*this) = b;}; B & operator= (const B & b); private: virtual void foo(); // and other private member variables and functions } I want to create an inheriting class D that will just override the...

How to have the Xcode 3.1 compiler warn of assignment operator in an if statement?

I've tried searching the documentation and the internet as best as I'm able, but I haven't been able to get the Xcode compiler to issue a warning if the assignment operator is used in an if statement. I'm coming from RealBasic, where I've got an extremely strong habit of typing this sort of comparison: if x = 5 then ... In C, of cour...

Reducing code duplication between operator= and the copy constructor

I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator? ...

Is it bad form to call the default assignment operator from the copy constructor?

Consider a class of which copies need to be made. The vast majority of the data elements in the copy must strictly reflect the original, however there are select few elements whose state is not to be preserved and need to be reinitialized. Is it bad form to call a default assignment operator from the copy constructor? The default assi...

operator= (T *r) in nested templates

Hi everybody. I have a problem concerning nested templates and the overriding of the assignment operator. Say i want to have a refcounting class template _reference. This _reference for now simply holds a pointer to the ref-counted object. The problem now is that this all works fine, as long as im doing this with simple classes or str...

Assignment operators in R: '=' and '<-'

What are differences in the assignment operators '=' and '<-' in R? I know that operators are slightly different as this example shows > x <- y <- 5 > x = y = 5 > x = y <- 5 > x <- y = 5 Error in (x <- y) = 5 : could not find function "<-<-" But is this the only difference? ...

C# += (plus equals) (Assignment by addition) working very slow, when string is too long?

Hello, I have a for loop and what I do is this. forloop ( loop 7000 times) { x += 2000_char_long_string; } Code lasts really long time in this forloop, maybe more than 1 minute. How can I solve this problem? Thanks. ...

Javascript String Assignment Operators

How come I can use += on a string, but I cannot use -= on it? For example... var test = "Test"; var arr = "&#8660;" test += arr; alert(test); // Shows "Test&#8660;" test -= arr; alert(test); // Shows "NaN" ...

Reference assignment operator in php =&

I am having a hard time determining what the =& (equals-ampersand) assignment operator does in PHP. Can anyone explain it? Is it deprecated? Thanks! ...

Default assignment operator in inner class with reference members

I've run into an issue I don't understand and I was hoping someone here might provide some insight. The simplified code is as follows (original code was a custom queue/queue-iterator implementation): class B { public: B() {}; class C { public: int get(); C(B&b) : b(b){}; private: B& b; }; ...

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

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. S...

Is this wrong or am i missing something ( int count = 10, x; )

Came across this example in the book im reading and it didn't make sense at all to me, I'm probably missing something but it seems like youre assigning count with the values '10' and then the value 'x' which isnt even an int. Just wondering if this is a syntax that is valid. The book says this: The variables count and x are declared to...

Any gotchas in copy ctor and assignment operator having slightly different semantics?

Please look at the following code and tell me if it's going to cause problems in the future, and if yes, how to avoid them. class Note { int id; std::string text; public: // ... some ctors here... Note(const Note& other) : id(other.id), text(other.text) {} void operator=(const Note& other) // returns void: no chaining ...

Why is the php assignment operator acting as an assignment by reference in this case?

I have some code that appears to behave differently between php4 and php5. This code below: class CFoo { var $arr; function CFoo() { $this->arr = array(); } function AddToArray($i) { $this->arr[] = $i; } function DoStuffOnFoo() { for ($i = 0; $i < 10; ++$i) { ...

Is there a BASIC dialect which uses "==" as the comparison operator?

Anyone who grew up on BASIC, and later switched to another language, had a real difficulty getting used to "(a == b)" rather than "(a = b)" to test for equality. Is there a dialect of BASIC which uses the "==" operator for comparisons rather than overloading "=" for assignments and comparisons? Or - and maybe this is stretching it - is ...

What's the difference between `=` and `<-` in R?

I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use? ...

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

So for binary operators on booleans, Java has &, |, ^, && and ||. Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For &, the result value is true if both operand values are true; otherwise, the result is false....

compiler generated constructors

This is just a quick question to understand correctly what happens when you create a class with a constructor like this: class A { public: A() {} }; I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to declar...

Copy constructor and dynamic allocation

I would like to ask you how to write a copy constructor (and operator = ) for the following classes. Class Node stores coordinates x,y of each node and pointer to another node. class Node { private: double x, y; Node *n; public: Node (double xx, double yy, Node *nn) : x(xx), y(yy), n(nn) {} void setNode (Node *nn) : n(nn) {} ... }; ...