initialization

What happens when a new object is created using another (existing) object?

巧了我就是萌 提交于 2021-02-08 09:45:13
问题 I read in a book, it says: when we initialize a newly created object using another - uses copy constructor to create a temporary object and then uses assignment operator to copy values to the new object! And later in the book I read: When a new object is initialized using another object, compiler creates a temporary object which is copied to the new object using copy constructor. The temporary object is passed as an argument to the copy constructor. Really confused, what actually happens!!

What is the faster: to create a new array or iterate through existing? [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 05:27:16
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I have an array, for example (in Java) int[] a = new int[N]; I have worked with it and now want to have array with zeros. What will

template Base Class initialization

谁说我不能喝 提交于 2021-02-07 18:26:48
问题 while in visual c++ the code below is accepted, g++ will generate the error: "class Derived does not have any field name Base" which is following the standard? template <class T> class Base { public: Base(){}; }; template <class T> class Derived:public Base<T> { public: Derived():Base(){} }; BTW: both accept Derived():Base<T>(){} so meantime, I will follow gcc 回答1: MSVC++ is not correct. Base is a template, not a type. Note that in the usual case, Base is looked up in the scope of Derived<T>

Initializing Generic Variables in Scala

ぃ、小莉子 提交于 2021-02-07 05:28:29
问题 How do I declare a generic variable in Scala without initializing it (or initializing to any value)? def foo[T] { var t: T = ???? // tried _, null t } 回答1: def foo[T] { var t: T = null.asInstanceOf[T] t } And, if you don't like the ceremony involved in that, you can ease it this way: // Import this into your scope case class Init() implicit def initToT[T](i: Init): T = { null.asInstanceOf[T] } // Then use it def foo[T] { var t: T = Init() t } 回答2: You can't not initialize local variables, but

C++ static global non-POD: theory and practice

限于喜欢 提交于 2021-02-06 22:14:34
问题 I was reading the Qt coding conventions docs and came upon the following paragraph: Anything that has a constructor or needs to run code to be initialized cannot be used as global object in library code, since it is undefined when that constructor/code will be run (on first usage, on library load, before main() or not at all ). Even if the execution time of the initializer is defined for shared libraries, you’ll get into trouble when moving that code in a plugin or if the library is compiled

C++ static global non-POD: theory and practice

北战南征 提交于 2021-02-06 22:01:17
问题 I was reading the Qt coding conventions docs and came upon the following paragraph: Anything that has a constructor or needs to run code to be initialized cannot be used as global object in library code, since it is undefined when that constructor/code will be run (on first usage, on library load, before main() or not at all ). Even if the execution time of the initializer is defined for shared libraries, you’ll get into trouble when moving that code in a plugin or if the library is compiled

C++ static global non-POD: theory and practice

点点圈 提交于 2021-02-06 22:00:29
问题 I was reading the Qt coding conventions docs and came upon the following paragraph: Anything that has a constructor or needs to run code to be initialized cannot be used as global object in library code, since it is undefined when that constructor/code will be run (on first usage, on library load, before main() or not at all ). Even if the execution time of the initializer is defined for shared libraries, you’ll get into trouble when moving that code in a plugin or if the library is compiled

Why use `ptr::write` with arrays of `MaybeUninit`s?

旧街凉风 提交于 2021-02-06 15:53:13
问题 In the standard library, the documentation shows how to instantiate arrays of MaybeUninit s: let arr: [MaybeUninit<T>; N] = MaybeUninit::uninit().assume_init(); We know this is safe because the contract of MaybeUninit allows for uninitialized values. Next we are asked to use ptr::write(value) to initialize each element. But this requires unsafe code once again. We also know that overwriting a MaybeUninit is safe, because it doesn't drop anything. So why not just overwrite it like arr[i] =

Swift Variables Initialization

谁说胖子不能爱 提交于 2021-02-06 11:37:39
问题 I have a question about variables initialization in swift. I have two ways to initialize a variable (as "property" of a class in Objective-C). Which of them is the most correct? class Class { var label: UILabel! init() { ... label = UILabel() ... } } or class Class { var label = UILabel() init() { … } } 回答1: Actually you have 5 ways to initialize properties. There is no correct way, the way depends on the needs. Basically declare objects like UILabel always – if possible – as constant ( let )

Swift Variables Initialization

为君一笑 提交于 2021-02-06 11:34:35
问题 I have a question about variables initialization in swift. I have two ways to initialize a variable (as "property" of a class in Objective-C). Which of them is the most correct? class Class { var label: UILabel! init() { ... label = UILabel() ... } } or class Class { var label = UILabel() init() { … } } 回答1: Actually you have 5 ways to initialize properties. There is no correct way, the way depends on the needs. Basically declare objects like UILabel always – if possible – as constant ( let )