Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters
Questions tagged [initializer]
684 questions
12
votes
2 answers
Initializing shared_ptr member variable, new vs make_shared?
When initializing a shared_ptr member variable:
// .h
class Customer
{
public:
Customer();
private:
std::shared_ptr something_;
}
// .cpp
Customer():
something_(new OtherClass())
{
}
vs.
Customer():
…

User
- 62,498
- 72
- 186
- 247
11
votes
3 answers
Debugging a C# Object Initializer
Does anyone have any tips for debugging exceptions in a C# object initializer block? The object initializer syntax is basically all or nothing, which can make it especially difficult to troubleshoot inside of a LINQ query. Short of breaking the…

technomalogical
- 2,982
- 2
- 26
- 43
11
votes
2 answers
C++ new if statement with initializer
The cppreference page for "if" statement;
https://en.cppreference.com/w/cpp/language/if
gives the following example;
Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if…

user3613174
- 669
- 1
- 7
- 13
11
votes
1 answer
Define ⊤ ("down tack") character as a constant I can use in my program
I'm trying to write some logical statements in Perl6.
I've made logical operators:
multi sub prefix:<¬> ($n) {
return not $n;
}
multi sub infix:<∧> ($n, $b) {
return ($n and $b);
}
multi sub infix:<∨> ($n, $b) {
return ($n or…

user6189164
- 667
- 3
- 7
11
votes
4 answers
Swift- variable not initialized before use (but it's not used)
Currently I've got some swift code like this:
class C {
let type: Type;
var num = 0;
init() {
self.type = Type({ (num: Int) -> Void in
self.num = num;
});
}
}
The Swift compiler refuses to permit it, saying that…

Puppy
- 144,682
- 38
- 256
- 465
11
votes
3 answers
What initializers should a MKAnnotationView subclass have in Swift?
I'm creating a subclass of MKAnnotationView in my project. It needs to have two properties for storing subviews which I need to initialize somewhere at the beginning.
MKAnnotationView has one initializer listed in its documentation,…

Kuba Suder
- 7,587
- 9
- 36
- 39
10
votes
3 answers
In what order are initializer block and variable definitions and etc. executed? (in java)
I have problem understanding the order in which initialization happens. this is the order I assumed:
*Once per
1. Static variable declaration
2. Static block
*Once per object
3. variable declaration
4. initialization block
5.…

Untitled
- 781
- 1
- 6
- 24
10
votes
4 answers
static initializers in objective C
How do I make static initializers in objective-c (if I have the term correct). Basically I want to do something like this:
static NSString* gTexts[] =
{
@"A string.",
@"Another string.",
}
But I want to do this more struct-like, i.e. have…

Joey
- 7,537
- 12
- 52
- 104
10
votes
1 answer
Java: ever seen a compiler or tool that REJECTS a final comma in array initializer?
My mystery begins like this. Consider this bit of code:
import java.util.Set;
import javax.annotation.processing.*;
import javax.lang.model.element.TypeElement;
@SupportedOptions({
"thing1",
"thing2",
})
public class fc extends…

Chapman Flack
- 604
- 5
- 13
10
votes
1 answer
Delegate a non-failable initializer to a failable initializer
Edit
A simple solutions was found thanks to @Airspeed Velocity, with a little twist added since this is parsed from JSON. Allow the initializer to take AnyObject? and default to Unknown (or Invalid):
init(value: AnyObject?) {
if let value = value…

Yariv Nissim
- 13,273
- 1
- 38
- 44
10
votes
2 answers
Double brace initializer and array
I have a method having an array parameter like:
public static void foo(int[] param) {
// Some code
}
And also I can call the method by writing like
foo(new int[3]);
Normally, we declare and initialize an array by new operator or double braces…

Ren lee
- 103
- 5
10
votes
2 answers
How do you test your Config/Initializer Scripts with Rspec in Rails?
So I was searching online for a solution, but there seems to be scarce information about testing initializers created in Rails.
At the moment, I've written a pretty large API call-n-store in my config/initializers/retrieve_users.rb file. It makes an…

ForgetfulFellow
- 2,477
- 2
- 22
- 33
10
votes
5 answers
How to initialize static pointer with malloc in C?
I'm trying to initiate a static variable (inside a function) with malloc in C, but I'm getting the "initializer not constant error". I know that I can't initiate a static with non constants in C, but can anyone think of a solution? I need the code…

Arash Fotouhi
- 1,933
- 2
- 22
- 43
10
votes
1 answer
Rails how to create an initializer inside a gem
I am trying to build a gem in Rails 3 and inside it i am trying to pass an initializer:
Credentials.configure do |config|
file = File.read("#{Rails.root}/config/twitter.yaml")
file_config = YAML.load(file)
config.consumer_key =…

Wahtever
- 3,597
- 10
- 44
- 79
10
votes
1 answer
Environment-specific initializers for rails?
Can you configure rails to only run an initializer under certain environments? In my case I had to hack paperclip to work with Imagemagick on my dev box, so I have monkeypatched code I want only to apply to the development environ, not the…

esilver
- 27,713
- 23
- 122
- 168