object-initialization

Why is a POD in a struct zero-initialized by an implicit constructor when creating an object in the heap or a temporary object in the stack?

回眸只為那壹抹淺笑 提交于 2019-11-28 17:30:17
The standard and the C++ book say that the default constructor for class type members is called by the implicit generated default constructor, but built-in types are not initialized. However, in this test program I get unexpected results when allocating an object in the heap or when using a temporary object: #include<iostream> struct Container { int n; }; int main() { Container c; std::cout << "[STACK] Num: " << c.n << std::endl; Container *pc = new Container(); std::cout << "[HEAP] Num: " << pc->n << std::endl; delete pc; Container tc = Container(); std::cout << "[TEMP] Num: " << tc.n << std:

Setting properties via object initialization or not : Any difference ?

半世苍凉 提交于 2019-11-28 08:54:06
Here's a simple question : Is there any (performance) difference between this : Person person = new Person() { Name = "Philippe", Mail = "phil@phil.com", }; and this Person person = new Person(); person.Name = "Philippe"; person.Mail = "phil@phil.com"; You can imagine bigger object with more properties. They are almost exactly equivalent except that the first method (using an object initializer ) only works in C# 3.0 and newer. Any performance difference is only minor and not worth worrying about. They produce almost identical IL code. The first gives this: .method private hidebysig instance

Setting properties via object initialization or not : Any difference ?

↘锁芯ラ 提交于 2019-11-27 02:30:06
问题 Here's a simple question : Is there any (performance) difference between this : Person person = new Person() { Name = "Philippe", Mail = "phil@phil.com", }; and this Person person = new Person(); person.Name = "Philippe"; person.Mail = "phil@phil.com"; You can imagine bigger object with more properties. 回答1: They are almost exactly equivalent except that the first method (using an object initializer) only works in C# 3.0 and newer. Any performance difference is only minor and not worth