Questions tagged [initializer]

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

684 questions
5
votes
1 answer

Expected initializer before namespace

So, I'm fairly new to C++ programming but I have used SDL extensively with python and FreeBASIC. I'm sure I'm missing something silly here, but no matter what I try I keep getting the error "error: expected initializer before ‘namespace’" in my…
Jsmith
  • 51
  • 1
  • 1
  • 5
5
votes
2 answers

JSF2 preRenderComponent is called always when f:ajax is executed

I have an JSF page backed by NewsBean.java which has as bean initializer. There is a button at the bottom of the page for sending comments which has:
Mohsen
  • 3,512
  • 3
  • 38
  • 66
5
votes
2 answers

How to prevent default initialization of a const variable with a class type

I have a custom class that I want to behave like a built-in type. However I have noticed that you can initialise a const variable of that class without providing an initial value. My class currently has an empty default constructor. Here is a…
ljbade
  • 4,576
  • 4
  • 30
  • 35
5
votes
3 answers

SwiftUI View: two different initializers: cannot convert value of type 'Text' to closure result type 'Content'

The code: import SwiftUI public struct Snackbar: View where Content: View { private var content: Content // Works OK public init(@ViewBuilder content: () -> Content) { self.content = content() } init(_ text:…
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
5
votes
2 answers

Advanced C question: Please explain C construct *({ foo(&bar); &bar; })

This is ultimately a C question that arose when studying code in completion.h of the Linux kernel source, where I see a C technique I've never used in C before. Although have a vague sense what it's doing, I'd like to fine tune my understanding with…
clearlight
  • 12,255
  • 11
  • 57
  • 75
5
votes
2 answers

How `const std::string& s = nullptr` works as an optional parameter

To my knowledge a reference cannot be null, but when I run code like this: #include #include void test(int i, const std::string& s = nullptr) { std::cout << i << " " << s << std::endl; } int main() { test(1, "test"); …
user2269707
5
votes
2 answers

Why does Swift disallow assignment to self in class init, but not in protocol init?

I often find myself liking how, in the initializers for structs, enums, and protocols, I can write something like self = someValue. This is great when I have some predefined values or am cloning an existing value. However, this syntax doesn't work…
Ky -
  • 30,724
  • 51
  • 192
  • 308
5
votes
2 answers

Referencing self in super.init

I have the following code (EDIT: Updated the code so everyone can compile it and see): import UIKit struct Action { let text: String let handler: (() -> Void)? } class AlertView : UIView { init(actions: [Action]) { …
Brandon
  • 22,723
  • 11
  • 93
  • 186
5
votes
1 answer

Subclassing factory methods of URLSession in Swift

I am trying to create a subclass of URLSession in Swift (reason does not matter, but has to do with testing). I need it to work with a delegate and a specific URLSessionConfiguration, which is a read-only property on URLSession. Usual way to…
Legoless
  • 10,942
  • 7
  • 48
  • 68
5
votes
1 answer

Swift: Self.init called multiple times in initializer

This one has me stumped. I can't figure out why Swift is complaining that self.init is called more than once in this code: public init(body: String) { let parser = Gravl.Parser() if let node = parser.parse(body) { …
devios1
  • 36,899
  • 45
  • 162
  • 260
5
votes
1 answer

Get class name in convenience init Swift 3

I'm trying to implement my own version of convenience init(context moc: NSManagedObjectContext), the new convenience initialiser on NSManagedObject in iOS 10. Reason being I need to make it compatible with iOS 9. I've come up with this: convenience…
Mark Bridges
  • 8,228
  • 4
  • 50
  • 65
5
votes
1 answer

Default value of a struct member

(I'm sure this question has already been answered, I'm just not sure on the right words to use to ask it. If someone would tell me what the correct terminology is that would be awesome!) I'm implementing a HashSet in C++ for a data structures class,…
bakester14
  • 153
  • 1
  • 4
  • 14
5
votes
2 answers

It shows "required initializer init must be provided in subclass of UIControl" When I override init(frame: CGRect)

This code works: import UIKit class wheel: UIControl { } But this code doesn't: class wheel: UIControl { override init(frame: CGRect) { super.init(frame: frame) } It shows error "required initializer init must be provided in subclass of…
allenlinli
  • 2,066
  • 3
  • 27
  • 49
5
votes
1 answer

How to initialize CBCentralManager in Swift when a self reference is necessary

What are good ways to initialize an instance of CBCentralManager, which requires a delegate and is often pointing to the owning class? I could declare the property as an implicitly unwrapped optional but doing so as a general practice seems rather…
Willam Hill
  • 1,572
  • 1
  • 17
  • 28
5
votes
1 answer

how to override objective-c method in swift

I am trying to override this objective-c method in a swift subclass. I keep getting compiler errors though: From AWSMTLModel - (instancetype)initWithDictionary:(NSDictionary *)dictionary error:(NSError **)error Swift subclass override func…
joels
  • 7,249
  • 11
  • 53
  • 94