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
7
votes
1 answer
C++ constructor initializer list throw exceptions
I have a problem with the following code. As we can see I have already handled the exception thrown by A's constructor in C's constructor, why should I bother to catch and handle the exception again in the main function?
#include
class…

JavaBeta
- 510
- 7
- 16
7
votes
7 answers
Java character array initializer
I tried to make a program that separates characters.
The question is:
"Create a char array and use an array initializer to initialize the array with the characters in the string 'Hi there'. Display the contents of the array using a for-statement.…

Ahmadz Issa
- 669
- 3
- 12
- 36
7
votes
3 answers
C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’
I've some warnings while trying to compile program in C language:
13:20: warning: excess elements in array initializer [enabled by default]
13:20: warning: (near initialization for ‘litera’) [enabled by default]
22:18: warning: excess elements in…

Naan
- 521
- 1
- 11
- 28
7
votes
6 answers
Is there any way to use an extension method in an object initializer block in C#
The simple demo below captures what I am trying to do. In the real program, I have to use the object initialiser block since it is reading a list in a LINQ to SQL select expression, and there is a value that that I want to read off the database and…

Anthony
- 5,176
- 6
- 65
- 87
6
votes
2 answers
Clang-format: how to format C struct initializer this way
I'm using the linux kernel .clang-format as a reference, but this part is bothering me.
How can I get clang-format to format this code
const struct my_struct hello = {.id = 0,
.modsmin = 10,
…

aganm
- 1,245
- 1
- 11
- 30
6
votes
3 answers
Java super-tuning, a few questions
Before I ask my question can I please ask not to get a lecture about optimising for no reason.
Consider the following questions purely academic.
I've been thinking about the efficiency of accesses between root (ie often used and often accessing each…
user595447
6
votes
5 answers
Do these two C++ initializer syntaxes ever differ in semantics?
Assume that the following code is legal code that compiles properly, that T is a type name, and that x is the name of a variable.
Syntax one:
T a(x);
Syntax two:
T a = x;
Do the exact semantics of these two expressions ever differ? If so, under…

Omnifarious
- 54,333
- 19
- 131
- 194
6
votes
3 answers
How to call Swift initializer methods with multiple param in Objective-C
I have One Initializer method in my Swift file like below:
public init(frame: CGRect, type: NVActivityIndicatorType? = nil, color: UIColor? = nil, padding: CGFloat? = nil) {
self.type = type ?? NVActivityIndicatorView.DEFAULT_TYPE
…

CodeChanger
- 7,953
- 5
- 49
- 80
6
votes
2 answers
Initializers from generic types won't be inherited in swift?
Here is my code:
public class A {
public init(n : Int) {
print("A")
}
}
public class B : A {
}
public class C : B {
}
let x = C(n: 123)
This fails compilation and yells such error:
repl.swift:9:9: error: 'C' cannot…

AntiMoron
- 1,286
- 1
- 11
- 29
6
votes
1 answer
How to implement NSManagedObject class conforming to Mappable
I have a class that maps directly JSON implementing the Mappable (ObjectMapper Framework) protocol and I'm trying to inherit from NSManagedObject.
class AbstractModel: NSManagedObject, Mappable {
@NSManaged var uuid: String?
@NSManaged var…

VincentS
- 602
- 1
- 10
- 24
6
votes
1 answer
Assigning let variable in fallible initializer swift 1.2
I have a struct with a fallible initializer, not an instance method, but an initializer. After updating to 1.2, when I try to assign a let property inside the initializer, I receive the following error Cannot assign to 'aspectRatio' in self. My code…

thank_you
- 11,001
- 19
- 101
- 185
6
votes
3 answers
Unable to programatically create a UIViewController in Swift
When I try to create an instance of a UIViewController in Swift, all the inherited initialisers are unavailable, even though I didn't define any designated inits in the view controller (or anything else, FWIW).
Also, if I try to display it by making…

cfischer
- 24,452
- 37
- 131
- 214
6
votes
1 answer
C++11: string(50, 'x') versus string{50, 'x'}
As seen on ideone:
cout << string(50, 'x'); // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
cout << string{50, 'x'}; // 2x
WAT??
I have figured out that 50 is ASCII '2', so:
cout << static_cast('2'); // 50
cout << static_cast(50);…

P i
- 29,020
- 36
- 159
- 267
6
votes
1 answer
How to initialize a C++ 11 standard container with regular constructor patterns?
Can the long explicit initializer list in the following be replaced by some template that generates it?
std::array foos = {{
{0, bar},
{1, bar},
{2, bar},
{3, bar},
{4, bar},
{5, bar},
…

not-a-user
- 4,088
- 3
- 21
- 37
5
votes
1 answer
using Rails to_prepare event
I'm trying to get the to_prepare event to work on a new Rails 3.2.1 project. I've placed the following:
Rails.application.config.to_prepare do
puts 'here i am before a request'
end
into an initializer under config/initializers. According to the…

operand
- 433
- 4
- 11