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
13 answers
Is there a way to define variables of two different types in a for loop initializer?
You can define 2 variables of the same type in a for loop:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
But it is illegal to define variables of different types:
int main() {
for (int i = 0, float…

Łukasz Lew
- 48,526
- 41
- 139
- 208
7
votes
2 answers
How often do initializers run in Rails?
Do the initializers in a Rails app run each time someone visits the site?
For example, if my server is started in Texas at 10 a.m. , and someone visits my site from New York at 1 p.m. and someone visits from Los Angeles at 10 p.m, do the the…

Leahcim
- 40,649
- 59
- 195
- 334
7
votes
2 answers
Rails 3.1: how to run an initializer only for the web app (rails server/unicorn/etc)
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…

sbutler
- 617
- 5
- 10
7
votes
1 answer
Why are const qualified variables accepted as initializers on gcc?
When compiling this code in latest verson of gcc (or clang) with -std=c17 -pedantic-errors -Wall -Wextra
static const int y = 1;
static int x = y;
then I get no compiler diagnostic message even though I'm fairly sure that this is not valid C but a…

Lundin
- 195,001
- 40
- 254
- 396
7
votes
1 answer
Object instantiation with curly braces and : symbols
Can you please explain the following object instantiation ?
How it is called ?
Where can I find more information about this kind of object instantiation ?
#include
#include
using namespace std;
class Car {
public:
string…

EmmanuelT
- 79
- 3
7
votes
1 answer
Why Railtie initializers are not executed?
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
…

Roman
- 13,100
- 2
- 47
- 63
7
votes
4 answers
How to initialize a variable when IBOutlet is initialized?
It happened for me to write following code snippet in one of my UIViewControllers in my new iOS Swift Application.
var starButtonsCount = starButtons.count
@IBOutlet var starButtons: [UIButton]!
Then straight next to the starButtonsCount variable…

Randika Vishman
- 7,983
- 3
- 57
- 80
7
votes
1 answer
How to set union in class initializer?
Given a class such as the one below and the given union, how does one initialize the union to the correct value?
What is being attempted here is to use two or more different types as one of the core data types for the class. Instead of using…

glenng
- 161
- 1
- 5
7
votes
2 answers
Initialize values of a struct pointer
I am starting to learn binary trees in cpp and dynamically allocated memories;
thus to initialize a struct I do this
struct node{
int val;
node* left;
node* right;
};
//Initialize:
node* root = new node;
root->val = 7;
root->left =…

polmonroig
- 937
- 4
- 12
- 22
7
votes
2 answers
Multiple Initializers in a single class (swift)
Create a Triangle class with properties to store the length of each side. Triangles are called scalene when all three sides are of different lengths, isosceles when two sides have the same length, or equilateral when all three sides have the same…

AllocSystems
- 1,049
- 2
- 11
- 16
7
votes
4 answers
Where to initialize target in a UIButton? Particularly caring for IBDesignable
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…

Fattie
- 27,874
- 70
- 431
- 719
7
votes
2 answers
Swift + Realm newbie: Problems with a simple Realm object and its initializers
I've been a long time Objective-C developer and heard about Realm some weeks ago. On the other hand I've always wanted to migrate little by little to Swift, so I created a small project involving Realm + Swift.
What does it mean? I'm a Swift + Realm…

Isaac
- 1,794
- 4
- 21
- 41
7
votes
2 answers
scala foreach and map initializers
Just seen an interesting possibility to initialize code blocks in Scala for high order functions such as foreach or map:
(1 to 3) map {
val t = 5
i => i * 5
}
(1 to 3) foreach {
val line = Console.readLine
i => println(line)
}
Is…

Slava Schmidt
- 296
- 2
- 12
7
votes
1 answer
Inline member initializer containing pointer to member
At work, I'm experimenting a bit to bring some reflection into our codebase. Basically what I want to achieve, is to capture a pointer to data-member inside the type of the data-member's initializer:
template

Erik Valkering
- 167
- 4
7
votes
1 answer
Swift's access control for default initializer
In Swift Programming Language guide, it says:
“The default initializer has the same access level as the type it
initializes.”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
https://itun.es/us/jEUH0.l
and then it says:
“For…

Boon
- 40,656
- 60
- 209
- 315