assignment-operator

var a, b, c = {}

一世执手 提交于 2019-12-13 18:17:55
问题 I thought the syntax: var a, b, c = {}; would mean that the three variables are separate, not references to the same {}. Is this because {} is an object and this is the standard behavior? So if I do: var a, b, c = 0; the three would indeed be separate and not references? Thanks, Wesley 回答1: They shouldn't be the same, no. Only c will be assigned the value. a and b would just be declared, but not initialized to anything (they'd be undefined). c would, as the only one of them, be initialized to

Can I declare an array first and give its value later? [duplicate]

限于喜欢 提交于 2019-12-13 05:57:46
问题 This question already has answers here : In C, why can't I assign a string to a char array after it's declared? (6 answers) Closed 4 years ago . I tried doing this but got an error. Why can't I do this? int main() { char sweet[5]; sweet = "kova"; printf("My favorite sweet is %s\n", sweet); return 0; } 回答1: Nope, you cant. Simply because array names are non-modifiable l-value . Which cannot be used as left operand in any expression. Therefore, you cannot keep it on the left side of a =

Probably simple but confusing assignment error

老子叫甜甜 提交于 2019-12-13 05:19:32
问题 #include <stdio.h> int main() { char gradesList[5]; gradesList[2] = "X"; printf("%c", gradesList[2]); } When I try to run this code I get these errors: Incompatible pointer to integer conversion Assignment makes integer from pointer without a cast 回答1: You have to assign a char not a pointer to a string literal . Use ' instead of " gradesList[2] = 'X'; In C string literals are represented using double qoutes, i.e. " . And char are represented using single quotes, i.e. ' . Since you have

I don't understand what's going on this syntax javascript [duplicate]

隐身守侯 提交于 2019-12-13 03:44:16
问题 This question already has answers here : Javascript object bracket notation ({ Navigation } =) on left side of assign (4 answers) Closed 2 years ago . How gonna assign in const { Types, Creators } in the below code I mean what Types gonna hold and what Creators gonna hold. const { Types, Creators } = createActions({ userRequest: ['username'], userSuccess: ['avatar'], userFailure: null }) var createActions = (function (config, options) { if (R.isNil(config)) { throw new Error('an object is

Template class assignment operator class

空扰寡人 提交于 2019-12-13 03:11:28
问题 I have a TemplateArray and a CharArray class. How do I make it so the templatearray's assignment operator only copies in from the chararray class when the templatearray is of the same type (I.E. char) or similar type (I.E. unsigned char) to chararray? TemplateArray and CharArray are functionally the same (except CharArray can handle NULL terminated strings). For example: template<typename TemplateItem> TemplateList & TemplateList<TemplateItem>::operator=(const CharArray &ItemCopy) { //How do

Proper Implementation of Copy Constructor and Equals Operator on a class with smart pointers

醉酒当歌 提交于 2019-12-13 02:28:58
问题 Suppose I want to implement a class which is copyable, so I can implement the copy constructor and assignment operator. However, what is the correct implementation and handling of unique and shared pointer variables? See this contrived example which has both types of pointers: Header File #include <memory> using std::unique_ptr; using std::shared_ptr; class Copyable { private: unique_ptr<int> uniquePointer; shared_ptr<int> sharedPointer; public: Copyable(); Copyable(int value); Copyable(const

Initializer lists and assignment overloading (operator =)

时光总嘲笑我的痴心妄想 提交于 2019-12-12 22:56:46
问题 Does the overloading of the assignment operator propagate to an initializer list? For example, suppose a class: class MyClass { private: std::string m_myString; //std::string overloads operator = public: MyClass(std::string myString); } And a constructor: MyClass::MyClass(std::string myString) : m_myString(myString) { } Will the initializer list work out the assignment operator overload on std::string ? And if not, is there a workaround? Particularly for GCC. 回答1: I think what you are missing

Why is the ++: operator in the Scala language so strange?

老子叫甜甜 提交于 2019-12-12 12:08:15
问题 I am using the ++: operator to get a collection of two collections, but the results I get using these two methods are inconsistent: scala> var r = Array(1, 2) r: Array[Int] = Array(1, 2) scala> r ++:= Array(3) scala> r res28: Array[Int] = Array(3, 1, 2) scala> Array(1, 2) ++: Array(3) res29: Array[Int] = Array(1, 2, 3) Why do the ++: and ++:= operators give different results? This kind of difference does not appear with the ++ operator. The version of Scala I am using is 2.11.8. 回答1: Since it

Are there any unexpected consequences of calling a destructor from the assignment operator?

对着背影说爱祢 提交于 2019-12-12 06:12:38
问题 For example: class Foo : public Bar { ~Foo() { // Do some complicated stuff } Foo &operator=(const Foo &rhs) { if (&rhs != this) { ~Foo(); // Is this safe? // Do more stuff } } } Are there any unexpected consequences of calling the destructor explicitly with regard to inheritance and other such things? Is there any reason to abstract out the destructor code into a void destruct() function and call that instead? 回答1: Calling the destructor is a bad idea in the simplest case, and a horrible one

Assignment Operator for an object

女生的网名这么多〃 提交于 2019-12-12 03:54:07
问题 I have written a code, for dynamically allocating a name. I know I should take care of deep copy in such scenarios. What I have written is my own version of Copy Constructor,Copy Assignment Operator and destructor. Should I redefine any other implicit functions such as Move Assignment Operator . I am not clear with the concept of Move Assignment Operator or any other implicitly defined member functions (other than which I have already mentioned ). Can any one please add the code for this