initializer

C++: newbie initializer list question

人走茶凉 提交于 2019-12-10 18:27:26
问题 Newbie here. I am looking at company code. It appears that there are NO member variables in class A yet in A's constructor it initializes an object B even though class A does not contain any member variable of type B (or any member variable at all!). I guess I don't understand it enough to even ask a question...so what's going on here!? My intuition is that you need a variable before you even try to initialize it. How is it possible (or what good does it do) to initialize an object without

Run an rspec “before” block before rails initializers run

风格不统一 提交于 2019-12-10 17:26:11
问题 I would like to run an rspec before block to set some stuff up before the Rails initializers run, so I can test what an initializer should be doing. Is this possible? 回答1: If the logic in your initializers is complex enough it should be tested, you should extract it into a helper that you can isolate and test without being in the context of the initializer. complex_initializer.rb config.database.foo = calculate_hard_stuff() config.database.bar = other_stuff() You could extract that into a

Where to initialize target in a UIButton? Particularly caring for IBDesignable

冷暖自知 提交于 2019-12-10 15:18:58
问题 I have a @IBDesignable class Fancy:UIButton I want to addTarget(self, action:#selector(blah), forControlEvents: UIControlEvents.TouchUpInside) So where in UIButton should that be done? Where is the best place for addTarget ? 1 - I have seen layoutSubviews suggested - is that right? Note - experimentation shows that a problem with layoutSubviews is that, of course, it can be called often, whenever things move around. It would be a bad idea to "addTarget" more than once. 2 - didMoveToSuperview

How to extend Array for generic type?

╄→гoц情女王★ 提交于 2019-12-10 10:59:12
问题 I have a class for a linked list declared like this: class LinkedNode<T> { let data: T var next: LinkedNode<T>? func traverseList(process: (LinkedNode<T>) -> ()) { ... } } What I want to do is extend Array to have an initialiser that converts my LinkedNode class to an array of linked nodes. I tried this: extension Array where Element == LinkedNode<T> { init(node: LinkedNode<T>) { var result = [LinkedNode<T>]() traverseList { result.append($0) } return result } } But that gives errors that T

Twitter integration rails 4 app

雨燕双飞 提交于 2019-12-10 00:10:17
问题 I'm attempting to integrate a twitter feed into my Rails 4 application using this gem. I've read the documentation on the github page but I'm running into a problem when trying to use a method in my view. I have the following in an initializer file named twitter_init.rb (with my auth info): client = Twitter::Streaming::Client.new do |config| config.consumer_key = "YOUR_CONSUMER_KEY" config.consumer_secret = "YOUR_CONSUMER_SECRET" config.access_token = "YOUR_ACCESS_TOKEN" config.access_token

C++ constructor initializer list throw exceptions

只愿长相守 提交于 2019-12-09 11:03:12
问题 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 <iostream> class WException : public std::exception { public: WException( const char* info ) : std::exception(info){} }; class A { public: A( int a ) : a(a) { std::cout << "A's constructor run." << std::endl; throw WException("A constructor throw exception."); }

need self to set all constants of a swift class in init

て烟熏妆下的殇ゞ 提交于 2019-12-09 07:34:55
问题 I have a Swift class that has a constant ivar (are they called instance constants now?). To set the value to this constant, I need to call an initializer of the desired object and pass itself. However, I am not allowed to as I need to initialize all values first, then call super.init() and after that I am allowed to access self . So what to do in this case? class Broadcaster: NSObject, CBPeripheralManagerDelegate { let broadcastID: NSUUID let bluetoothManager: CBPeripheralManager init

Why Railtie initializers are not executed?

£可爱£侵袭症+ 提交于 2019-12-08 17:24:38
问题 While crafting the Passenger-Monit plugin, I thought that it'll be most appropriate to use the initializer, i.e. module PassengerMonit class Railtie < Rails::Railtie initializer "my_plugin.some_init_task" do # my initialization tasks end end end However, for some reason the code in the block was never executed. I finally made a work-around with config.before_initialize {} but I'm curious, why the initializer wasn't executed. What could have prevented it from running? 回答1: In your gemfile, did

Rails 3.1: how to run an initializer only for the web app (rails server/unicorn/etc)

a 夏天 提交于 2019-12-07 00:57:17
问题 My webapp needs to encrypt its session data. What I setup is: config/initializers/encryptor.rb: require 'openssl' require 'myapp/encryptor' MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 ) Session.delete_all app/models/session.rb: require 'attr_encrypted' class Session < ActiveRecord::Base attr_accessible :session_id, :data attr_encryptor :data, :key => proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true # Rest of model stuff end That all works great,

Why doesn't initializer work with properties returning list<t>?

淺唱寂寞╮ 提交于 2019-12-06 23:52:19
问题 Couldn't find an answer to this question. It must be obvious, but still. I try to use initializer in this simplified example: MyNode newNode = new MyNode { NodeName = "newNode", Children.Add(/*smth*/) // mistake is here }; where Children is a property for this class, which returns a list. And here I come across a mistake, which goes like 'Invalid initializer member declarator'. What is wrong here, and how do you initialize such properties? Thanks a lot in advance! 回答1: You can't call methods