assignment-operator

In the expression left() = right(), why is right() sequenced first?

眉间皱痕 提交于 2019-12-05 01:42:25
In C++, the expression left() = right() evaluates right() left() in that sequence. The right() goes first, as has been discussed here. I cannot think of a reason for right() to go first. Can you? I presume that there exists a reason. Otherwise, the standard would hardly say what it says, but consider: right() will return some result. At the machine-code level, does the CPU not need to know where to put the result right() will return before asking right() to return it? If you happen to know what the standard committee was thinking (because you were in the room or have read the memo), that's

Constructor or Assignment Operator

限于喜欢 提交于 2019-12-05 00:17:42
问题 Can you help me is there definition in C++ standard that describes which one will be called constructor or assignment operator in this case: #include <iostream> using namespace std; class CTest { public: CTest() : m_nTest(0) { cout << "Default constructor" << endl; } CTest(int a) : m_nTest(a) { cout << "Int constructor" << endl; } CTest(const CTest& obj) { m_nTest = obj.m_nTest; cout << "Copy constructor" << endl; } CTest& operator=(int rhs) { m_nTest = rhs; cout << "Assignment" << endl;

Is there a way to customize/override assignment operations in JavasScript?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 23:26:57
Every time I assign a string , I'd actually like to assign a string object , without the extra code. This var foo = "bar"; becomes var foo = new String("bar"); Basically hi-jacking the assignment. Follow-up: If the above is not possible is there a way to prototype the string variable type, rather than the String object? As pointed out by armando , the foo would be a string type, but is essentially a customized array. It would be nice to be able to prototype functions to that class. No this is not possible If it was possible, you really would not want to do this, at least not globally. The

Order of assignment vs order of initialization

南笙酒味 提交于 2019-12-04 22:20:48
Take this example code: int a = 10; int b = 20; int c = 30; int & foo1() { qDebug() << "foo1" << endl; return a; } int & foo2() { qDebug() << "foo2" << endl; return b; } int & foo3() { qDebug() << "foo3" << endl; return c; } int main(void) { foo1() = foo2() = foo3() = 7; } Since assignment goes right to left, I expected to see foo3 first and foo1 last, but it is the opposite. Are the rules for such scenarios concretely defined and how? Also, does the compiler differentiate between assignment and other operators and how would that be possible if you are using the = operator in a different

What does Swift's optional binding do to the type it's arguments?

天大地大妈咪最大 提交于 2019-12-04 22:08:43
Why is if let y: Int? = nil { ... } the same as if let y: Int? = nil as Int?? { ... } (and thus an invalid assignment) especially when, on its own let y: Int? = nil is not the same as let y: Int? = nil as Int?? (since let y: Int? = nil is a valid assignment)? OK, I will answer, with my poor English skills ;-) Let's start with this: if let lvalue:T = rvalue { ... } At first the compiler tries to convert rvalue to T? by wrapping with Optional . For example: typealias T = Int let rvalue:Int? = 1 if let lvalue:T = rvalue { ... } // do nothing because `rvalue` is already `T?` //--- typealias T =

“Almost default” copy constructor (& assignment operator) in C++

孤人 提交于 2019-12-04 18:36:48
问题 A common thing I find myself doing is making "almost default" copy constructors and assignment operators. That is, I find myself in situations where the compiler supplied copy and assignment operators would work for most of the data members, but there's a particular data member which needs to be handled differently. This means that I have to explicitly create a copy constructor/assignment operator, including explicitly listing all the data members which have simple copy semantics. This can

Do derived classes indirectly inherit base's assignment operator?

倖福魔咒の 提交于 2019-12-04 13:44:22
I'm trying to understand this behaviour but it seems I don't. Please see this code: #include <iostream> using namespace std; class Base { public: void operator=(const Base& rf) { cout << "base operator=" << endl; this->y = rf.y; } int y; Base() : y(100) { } }; class Derived : public Base { public: int x; Derived() : x(100) { } }; int main() { Derived test; Derived test2; test2.x = 0; test2.y = 0; test.operator=(test2); // operator auto-generated for derived class but... cout << test.x << endl << test.y << endl; cin.ignore(); return 0; } PROGRAM OUTPUT: > base operator= > 0 > 0 Now where I'm

Error when have private copy ctor with public assignment operator

北城余情 提交于 2019-12-04 12:10:51
问题 Can one of you explain why the following piece of code does not compile? #include <iostream> using namespace std; class Foo { public: Foo() { cout << "Foo::Foo()" << endl << endl; } Foo& operator=(const Foo&) { cout << "Foo::operator=(const Foo&)" << endl << endl; } private: Foo(const Foo& b) { *this = b; cout << "Foo::Foo(const Foo&)" << endl << endl; } }; int main() { Foo foo; foo = Foo(); } The error I receive: $ g++ -o copy_ctor_assign copy_ctor_assign.cc && ./copy_ctor_assign copy_ctor

Constant class members, assignment operator and QList

◇◆丶佛笑我妖孽 提交于 2019-12-04 07:38:57
Please conform if I am correct and tell me whether there is a better solution: I understand that objects with constant members like int const width; can not be handled by the synthetic assignment operator that is implicitly created by the compiler. But QList (and I suppose std::list, too) needs a working assignment operator. So when I want to use objects with constant members and QList I have three possibilities: Don't use constant members. (Not a solution) Implement my own assignment operator. Use some other container that does not need assignment operators Is that correct? Are there other

Is chained assignment in C/C++ undefined behavior?

不打扰是莪最后的温柔 提交于 2019-12-04 06:21:12
Ignoring the types of variables, is expression like a=b=c has defined behavior in both C and C++? If so, can any one give me official evidence, like quotes from the standard, please? P.S. I searched the chained assignment but everything I got is associativity, but I didn't find any text about that in the C99 standard. Maybe I did it wrong? hoping anyone can help me. From the C++ Standard 5.17 Assignment and compound assignment operators [expr.ass] 1 The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand