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
3
votes
1 answer

can an instance method be called from initializer block?

I've been brushing up on Java and I've read that "initializer block can call methods" Can it call instance methods despite the constructor not having executed? Is this just frowned upon? EDIT: I see the compiler allows it, so the question is, is it…
Vlad Ilie
  • 1,389
  • 1
  • 13
  • 37
3
votes
2 answers

C++ Array Initializers Warnings

I have declared and initialized a constant char array within a class: class grid { const char test[11] = {'s', 'e', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; My code works, but I keep getting compiler warnings: non static data member…
ggle
  • 426
  • 3
  • 20
3
votes
3 answers

Using Initializer Syntax on a C# Property

In C#, this is valid syntax: int[] numbers = {1, 2, 3, 4, 5}; I'm trying to use similar syntax with a property on my object: MyClass myinst = new MyClass(); // See Class Definition below myinst.MinMax = {-3.141, 3.141}; //…
abelenky
  • 63,815
  • 23
  • 109
  • 159
3
votes
5 answers

C++: newbie initializer list question

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…
jbu
  • 15,831
  • 29
  • 82
  • 105
3
votes
1 answer

Objective C: init and awakeFromNib

I've recently studied some Cocoa based open source projects. I saw that a lot of programs have all initializing code in awakeFromNib and rarely use the designated initializer. I am used to do it that way: in the overridden designated initializer:…
user1360618
3
votes
2 answers

Invalid Initializer in C

I'm a beginner in the C Programming language and I wanted to write a hashing program. I can write this program with a specified number of typedef ... Name elemenst (in an array) but, when I use dynamic allocation, an "invalid initializer" error…
Otavio Mota
  • 31
  • 1
  • 1
  • 5
3
votes
2 answers

Interface collection member strange behavior during object initialization

I run into runtime NullReferenceException exception in the following code: public class Container { public IList Items { get; set; } } class Program { static void Main(string[] args) { var container = new Container() {…
Andrew
  • 1,102
  • 1
  • 8
  • 17
3
votes
2 answers

About collection initializers in c#

I admit I'm far from experienced with c#, so this may be obvious, but I have to ask -- is there any difference between the two code samples? In case it's not obvious, the first statement omits () at the end of new operator. Is there any difference…
ADSMarko
  • 363
  • 1
  • 3
  • 8
3
votes
2 answers

Must an Objective-C class have exactly one designated initializer?

I found some info of the designated initializer in this Apple's docs, but what I don't understand is, must each class have one and only one designated initializer? For example, what if class A has initL, initM, initN, while class B inherits from…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
3
votes
3 answers

Strange code on Java Static Initialization Blocks

When going through the JLS 8.3.2.3 I wasn't able to understand the following code. class Z { static { i = j + 2; } static int i, j; static { j = 4; } } The code is resulting in the error Cannot reference a field before it is defined But if I change…
Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
3
votes
3 answers

Resolve a circular static reference

I have 2 static initializers trying to call each other: one in a Configuration class, that reads configuration parameters from a properties file; one in a LoggerFactory class, which sets up the default logging parameters. The problem is they…
assylias
  • 321,522
  • 82
  • 660
  • 783
3
votes
5 answers

TCPL 5.9.9 (C++): Where would it make sense to use a name in its own initializer?

This is a question from the most recent version of Stroustrup's "The C++ Programming Language". I've been mulling this over in my head for the past couple days. The only thing I can come up with, (and this is probably incorrect) is something like…
Joe Snikeris
  • 1,150
  • 12
  • 17
3
votes
2 answers

template error: expected initialiser before 'template'

Hi stackoverflow forum people, I've typed up this code direct from the text book, Absolute C++ Fourth Edition Savitch ISBN-13: 978-0-13-136584-1. A Generic Sorting Function. sort.cpp on page 728 gives the error on line 17: Line 17: error: expected…
user1416486
  • 61
  • 2
  • 7
3
votes
4 answers

Does calling a C++ constructor from another member function/constructor execute the initializer list?

In a C++ object, when you call a constructor from another constructor or member function (after the object has already been constructed), does the initializer list of the constructor you're calling still get executed?
user553702
  • 2,819
  • 5
  • 23
  • 27
3
votes
2 answers

adding a scope to ActiveRecord::Base via initializer?

I've tried to add a scope like this, via an initializer class ActiveRecord::Base scope :this_month, lambda { where(:created_at => Time.now.beginning_of_month..Time.now.end_of_month) } end But Im getting the error "NoMethodError:…
pixelearth
  • 13,674
  • 10
  • 62
  • 110