Scalar `new T` vs array `new T[1]`

If T doesn’t have trivial destructor, then for usual compiler implementations, new T[1] has an overhead compared to new T. The array version will allocate a little bit larger memory area, to store the number of elements, so at delete[], it knows how many destructors must be called. So, it has an overhead: a little … Read more

Setup std::vector in class constructor

Just do: MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0) You already seem to know about initializer lists, why not initialize vector there directly? vec = new vector<int>(size,0); is illegal because new returns a pointer and in your case vec is an object. Your second option: vector<int> temp(size,0); vec = temp; although it compiles, does extra work … Read more

How do I override, not hide, a member variable (field) in a C# subclass?

You cannot override variables in C#, but you can override properties: public class Item { public virtual string Name {get; protected set;} } public class Subitem : Item { public override string Name {get; protected set;} } Another approach would be to change the value in the subclass, like this: public class Item { public … Read more

Where and why use int a=new int?

static void Main() { int A = new int(); int B = default(int); int C = 100; Console.Read(); } Is compiled to .method private hidebysig static void Main() cil managed { .entrypoint // Code size 15 (0xf) .maxstack 1 .locals init ([0] int32 A, [1] int32 B, [2] int32 C) IL_0000: nop IL_0001: ldc.i4.0 IL_0002: … Read more