Shallow copy of a hashset

Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary. It’s easy to create your own of course: public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source) { if (source == null) { throw new ArgumentNullException(“source”); } return new HashSet<T>(source); } (With … Read more

Shallow copy or Deep copy?

From the link here Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements. Deep copies duplicate everything. A deep copy of a collection is two collections with all of the … Read more

Can I use memcpy in C++ to copy classes that have no pointers or virtual functions

According to the Standard, if no copy constructor is provided by the programmer for a class, the compiler will synthesize a constructor which exhibits default memberwise initialization. (12.8.8) However, in 12.8.1, the Standard also says, A class object can be copied in two ways, by initialization (12.1, 8.5), including for function argument passing (5.2.2) and … Read more

Does Object.assign() create a deep copy or a shallow copy?

Forget about deep copy, even shallow copy isn’t safe, if the object you’re copying has a property with enumerable attribute set to false. MDN : The Object.assign() method only copies enumerable and own properties from a source object to a target object take this example var o = {}; Object.defineProperty(o,’x’,{enumerable: false,value : 15}); var ob={}; … Read more

When should I pass or return a struct by value?

On small embedded architectures (8/16-bitters) — always pass by pointer, as non-trivial structures don’t fit into such tiny registers, and those machines are generally register-starved as well. On PC-like architectures (32 and 64 bit processors) — passing a structure by value is OK provided sizeof(mystruct_t) <= 2*sizeof(mystruct_t*) and the function does not have many (usually … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)