Questions tagged [initialization]

Initialization deals with the task of initializing the contents of your data structure. It's a common practice in statically-typed languages.

Initialization deals with the task of initializing the contents of data structures. This is the initialization of an object, structure, blob of data, or any entity which contains state that must be prepared prior to usage of the entity.

11649 questions
103
votes
4 answers

Adding code to __init__.py

I'm taking a look at how the model system in django works and I noticed something that I don't understand. I know that you create an empty __init__.py file to specify that the current directory is a package. And that you can set some variable in…
Erik
  • 1,674
  • 3
  • 15
  • 18
102
votes
5 answers

Can PHP instantiate an object from the name of the class as a string?

Is it possible in PHP to instantiate an object from the name of a class, if the class name is stored in a string?
user135295
  • 1,023
  • 2
  • 7
  • 5
101
votes
2 answers

Default value of BOOL

What is the default value of a BOOL variable in Objective-C?
suse
  • 10,503
  • 23
  • 79
  • 113
101
votes
8 answers

Initializing a dictionary in python with a key value and no corresponding values

I was wondering if there was a way to initialize a dictionary in python with keys but no corresponding values until I set them. Such as: Definition = {'apple': , 'ball': } and then later i can set them: Definition[key] = something I only want to…
user2989027
  • 1,355
  • 3
  • 9
  • 13
101
votes
3 answers

C++11 allows in-class initialization of non-static and non-const members. What changed?

Before C++11, we could only perform in-class initialization on static const members of integral or enumeration type. Stroustrup discusses this in his C++ FAQ, giving the following example: class Y { const int c3 = 7; // error: not…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
99
votes
8 answers

JUnit: using constructor instead of @Before

I'm using JUnit 4. I can't see the difference between initializing in the constructor or using a dedicated init function annotated by @Before. Does this mean that I don't have to worry about it? Is there any case when @Before gives more than just…
vbence
  • 20,084
  • 9
  • 69
  • 118
99
votes
14 answers

Custom init for UIViewController in Swift with interface setup in storyboard

I'm having issue for writing custom init for subclass of UIViewController, basically I want to pass the dependency through the init method for viewController rather than setting property directly like viewControllerB.property = value So I made a…
Pigfly
  • 991
  • 1
  • 6
  • 3
98
votes
8 answers

memset() or value initialization to zero out a struct?

In Win32 API programming it's typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zeroed out. This can be achieved in either of the two ways: STRUCT theStruct; memset(…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
97
votes
8 answers

How to check 'late' variable is initialized in Dart

In kotlin we can check if the 'late' type variables are initialized like below lateinit var file: File if (this::file.isInitialized) { ... } Is it possible to do something similar to this in Dart..?
towhid
  • 2,778
  • 5
  • 18
  • 28
96
votes
8 answers

Is it possible to initialize a C pointer to NULL?

I had been writing things like char *x=NULL; on the assumption that char *x=2; would create a char pointer to address 2. But, in The GNU C Programming Tutorial it says that int *my_int_ptr = 2; stores the integer value 2 to whatever random…
fagricipni
  • 927
  • 1
  • 7
  • 9
96
votes
7 answers

Why can't Swift initializers call convenience initializers on their superclass?

Consider the two classes: class A { var x: Int init(x: Int) { self.x = x } convenience init() { self.init(x: 0) } } class B: A { init() { super.init() // Error: Must call a designated initializer of…
Robert
  • 5,735
  • 3
  • 40
  • 53
95
votes
4 answers

Swift: Custom ViewController initializers

How do you add custom initializers to UIViewController subclasses in Swift? I've created a sub class of UIViewController that looks something like this: class MyViewController : UIViewController { init(leftVC:UIViewController,…
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
94
votes
12 answers

In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?

Internally and about the generated code, is there a really difference between : MyClass::MyClass(): _capacity(15), _data(NULL), _len(0) { } and MyClass::MyClass() { _capacity=15; _data=NULL; _len=0 } thanks...
Stef
  • 3,691
  • 6
  • 43
  • 58
94
votes
11 answers

Default values and initialization in Java

Based on my reference, primitive types have default values and Objects are null. I tested a piece of code. public class Main { public static void main(String[] args) { int a; System.out.println(a); } } The line…
93
votes
11 answers

Default values in a C Struct

I have a data structure like this: struct foo { int id; int route; int backup_route; int current_route; } and a function called update() that is used to request changes in it. update(42, dont_care, dont_care, new_route); this is…
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284