initializer

Objective-C: init vs initialize

这一生的挚爱 提交于 2019-11-27 05:03:18
问题 In Objective-C, what is the difference between the init method (i.e. the designated initializer for a class) and the initialize method? What initialization code should be put in each? 回答1: -init is an instance method, used to initialize a particular object. +initialize is a class method, run before any instances of the class are created and before other class methods are run. +initialize isn't something you use most of the time, but it's handy for setting up any static variables that the

Can I declare variables of different types in the initialization of a for loop? [duplicate]

亡梦爱人 提交于 2019-11-27 04:30:19
This question already has an answer here: Is it possible to declare two variables of different types in a for loop? 7 answers Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the declaration of two variables of different types inside the for-loop initializer prohibited? If so, how can you work around it? Yes, that is prohibited. Just as otherwise you cannot declare variables of differing types in one declaration statement ( edit : modulo the declarator modifiers that

Initializer does not override a designated initializer from its superclass

我的未来我决定 提交于 2019-11-27 03:59:37
问题 So I've just upgraded to Xcode 6.3 Beta 3 and a lot of error(s) are appearing relating to the following: Initializer does not override a designated initializer from its superclass. override init() { super.init() } For example this is a UIButton class: class CustomButton: UIButton { var target: AnyObject! var selector: Selector! var action: (() -> Void)! override init() { // Initializer does not override a designated initializer from its superclass super.init() // Must call a designated

Initializing shared_ptr member variable, new vs make_shared?

两盒软妹~` 提交于 2019-11-27 03:31:41
问题 When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr<OtherClass> something_; } // .cpp Customer(): something_(new OtherClass()) { } vs. Customer(): something_(std::make_shared<OtherClass>()) { } Is the make_shared version allowed? I always seem to see the first version, which is preferred? 回答1: The only times when make_shared is not allowed are: If you're getting a naked pointer allocated by someone else and storing it in shared

Char array initialization dilemma

☆樱花仙子☆ 提交于 2019-11-26 22:06:06
问题 Consider following code: // hacky, since "123" is 4 chars long (including terminating 0) char symbols[3] = "123"; // clean, but lot of typing char symbols[3] = {'1', '2', '3'}; so, the twist is actually described in comment to the code, is there a way to initialize char[] with string literal without terminating zero? Update : seems like IntelliSense is wrong indeed, this behaviour is explicitly defined in C standard. 回答1: This char symbols[3] = "123"; is a valid statement. According to the

Calling a Java method with no name

◇◆丶佛笑我妖孽 提交于 2019-11-26 21:22:19
I'm looking at the code below and found something a bit strange: public class Sequence { Sequence() { System.out.print("c "); } { System.out.print("y "); } public static void main(String[] args) { new Sequence().go(); } void go() { System.out.print("g "); } static { System.out.print("x "); } } I would've expected this to give a compilation error as the System.out with "y " doesn't belong to a method declaration just a { } . Why is this valid? I don't see how this code would or should be called. When running this it produces x y c g also, why does the static { } get called before the sequence

Is an empty initializer list valid C code?

我们两清 提交于 2019-11-26 20:35:45
It is common to use {0} to initialize a struct or an array but consider the case when the first field isn't a scalar type. If the first field of struct Person is another struct or array, then this line will result in an error ( error: missing braces around initializer ). struct Person person = {0}; At least GCC allows me to use an empty initializer list to accomplish the same thing struct Person person = {}; But is this valid C code? Also: Is this line guaranteed to give the same behavior, i.e. a zero-initialized struct ? struct Person person; interjay No, an empty initializer list is not

Subclassing NSWindowController in Swift and init(windowNibName)

拜拜、爱过 提交于 2019-11-26 20:08:27
问题 I am trying to start a new document based Cocoa project in Swift and want to create a subclass of NSWindowController (as recommended in Apple's guides on document based apps). In ObjC you would make an instance of an NSWindowController subclass sending the initWithWindowNibName: message, which was implemented accordingly, calling the superclasses method. In Swift init(windowNibName) is only available as an convenience initializer, the designated initializer of NSWindowController is init

static readonly field initializer vs static constructor initialization

a 夏天 提交于 2019-11-26 19:18:34
问题 Below are two different ways to initialize static readonly fields. Is there a difference between the two approaches? If yes, when should one be preferred over the other? class A { private static readonly string connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } class B { private static readonly string connectionString; static B() { connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } } 回答1: There is

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

喜欢而已 提交于 2019-11-26 18:31:39
What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. Jörg W Mittag In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually, this is obj.send(:initialize, …) because initialize is private obj end end But you can, of