assignment-operator

C# Class as one Value in Class // Mimic int behaviour

北城以北 提交于 2019-12-25 19:10:43
问题 Is there a way to make my new Class mimic the behavior of an int or any Valuetype? I want something, so these assignments are valid MyClass myClass = 1; int i = myClass; var x = myClass; // And here is important that x is of type int! The MyClass looks roughly like this public class MyClass { public int Value{get;set;} public int AnotherValue{get;set;} public T GetSomething<T>() {..} } Every assignment of MyClass should return the Variable Value as Type int. So far i found implicit operator

copy constructor,destructor and temporaries

痞子三分冷 提交于 2019-12-25 04:29:10
问题 I wrote this class to test the behaviour of the default constructor,the copy constructor, the assignment operator and the destructor: #include <iostream> class Test { public: Test(); Test(const Test&); ~Test(); Test &operator=(const Test&); private: static int count; int label; }; Test::Test() : label(count++) { std::cout<<"constructor of "<<label<<std::endl; } Test::Test(const Test &other) : label(count++) { std::cout<<"copy-constructor of "<<label<<std::endl; } Test::~Test() { std::cout<<

Python simultaneous assign only from some elements of a list

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:38:27
问题 In Python I can do this w,x,y,z = (1,1,2,3) But suppose I only need the values of x and y. Is there a way to only simultaneously assign a few variables (while still keeping the beautiful simultaneous assignment syntax?). I'm hoping to find something along the lines of MATLAB's tilde operator ~,x,y,~ = (1,1,2,3) # This is not valid Python code I'm aware that I can just define a dummy variable to do this, d,x,y,d = (1,1,2,3) But I am curious if there is a special operator just for this purpose.

Javascript: How can I mix in methods of other Objects B, C to my Object A without copying but with linking?

扶醉桌前 提交于 2019-12-24 15:27:24
问题 If I create an Object A: let A = {}; And want to mix in methods from other Objects B and C: let B = { foo() { alert("Boo!"); } }; let C = { bar() { alert("No!"); } }; Normally I would call: Object.assign(A, B, C); Then I change my function foo: Object.assign(B, { foo() { alert("Hooray!"); } }); Objcect.assign(C, { bar() { alert("Yes!"); } }); After that I call foo or bar: A.foo(); // Actual output: "Boo!", desired: "Hooray!" A.bar(); // Actual output: "No!", desired: "Yes!" So far I found out

Why doesn't this vector assignment work?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 15:14:44
问题 Similar Questions: STL vector reserve() and copy() std::vector reserve() and push_back() is faster than resize() and array index, why? std::vector::resize() vs. std::vector::reserve() #include <vector> #include <iostream> using namespace std; int main() { vector<vector<int> > vvi; vvi.resize(1); vvi[0].reserve(1); vvi[0][0] = 1; vector<int> vi = vvi[0]; cout << vi[0]; // cout << vvi[0][0]; works return 0; } This gives me a seg fault, and I can't tell why. 回答1: vvi[0].reserve(1); vvi[0][0] = 1

Assigning passive values or constants to a user-defined type

非 Y 不嫁゛ 提交于 2019-12-24 07:49:48
问题 So I'm working on an automatic differentiation toolbox in Fortran using operator overloading. I have previously implemented this in C++ but really need to get it to work in Fortran. I have the following module defined in Fortran: module adopov integer :: indexcount integer, parameter :: tape_size = 1000 ! !....... ADtype public :: ADtype type ADtype integer :: index = -1 real :: v = 0.0 ! contains procedure :: oo_asg generic, public :: assignment(=) => oo_asg end type ADtype ! !....... class

Difference between C and in C++ regarding the “^=” operator

吃可爱长大的小学妹 提交于 2019-12-23 20:32:27
问题 I want to exchange the values pointed to by int *x and by int *y using the expression *x ^= *y ^= *x ^= *y; (Well,I know this expression is awkward, and I just want to know the difference, no offense.) This worked in C++, but failed in C. However if I divide it into three parts, like below *x ^= *y; *y ^= *x; *x ^= *y; It works fine for both languages. So, what are the difference of the operator ^= in C and C++? 回答1: The difference is not in the pointers as you initially suspected, but in the

Special member functions in C++0x

落爺英雄遲暮 提交于 2019-12-23 19:47:43
问题 The Wikipedia article about special member functions doesn't contain any reference to move constructors and move assignment operators. I would like to update the entry but I'm not sure what the 0x standard says. What are the rules regarding these two functions? Are they automatically generated by the compiler and if so when? Edit: I've updated the Wikipedia page, if anyone feels like it please help the community by editing it into shape (if needed). 回答1: Keeping in mind C++0x isn't quite

Why does move constructor affect is_assignable?

荒凉一梦 提交于 2019-12-23 17:53:12
问题 Just came from is_assignable and std::unique_ptr. @Angew tells me that because std::unique_ptr<int, do_nothing> and std::unique_ptr<int> are different types, so static_assert(not std::is_assignable<std::unique_ptr<int>, std::unique_ptr<int, do_nothing>>::value, ""); . So, I tried: template<typename T, typename D> struct MoveAssignOnly_V2 { MoveAssignOnly_V2& operator=(MoveAssignOnly_V2&) = delete; MoveAssignOnly_V2& operator=(MoveAssignOnly_V2&&) noexcept {} }; int main() { static_assert(not

c++ default assignment operator

人走茶凉 提交于 2019-12-23 09:05:49
问题 int a[10]; int b[10]; a = b; // struct test { int a[10]; }; test a,b; a = b; First code doesn't compile, since we cant assign array, but the second does. Isn't the default assignment operator of class simply call assignment for each data members? Why does the the second code compile? 回答1: From the C++11 draft, section 12.8: The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy/move assignment of its subobjects. The direct base classes of X are