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
10
votes
6 answers

Why do I get an error about the initializer not being a constant?

I am using the following code. const int X_ORIGIN = 1233086; const int Y_ORIGIN = -4728071; const int Z_ORIGIN = 4085704; const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN}; When I compile it, GCC gives…
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
9
votes
1 answer

Declare a Table Variable Based on Select Statement

I want to declare a table variable and fill it with a select, without having to explicitly define its columns. Does T-SQL allow something like this: DECLARE @people TABLE() SELECT * FROM Persons; Hypothetically, the above statement would match…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
9
votes
5 answers

Should I use the initializer list or perform assignments in my C++ constructors?

class Node { public: Node *parent; // used during the search to record the parent of successor nodes Node *child; // used after the search for the application to view the search in reverse float g; // cost of this node + it's…
Dzung Nguyen
  • 9,152
  • 14
  • 65
  • 104
9
votes
2 answers

Why Swift requires override of designated initializer of generic superclass?

According to Apple's documentation Swift does not necessary require override of initializer. In a following code example Bar inherits initializer of Foo: class Foo { let value: Int init(value: Int = 5) { self.value = value } } class Bar:…
Nikita Leonov
  • 5,684
  • 31
  • 37
9
votes
1 answer

Member initializer list in definition or declaration?

Should I declare the member initializer list for a class in the constructor declaration: class A { public: A(int data) : theData(data); }; or in the constructor definition: A::A(int data) : theData(data) { // code... }; or does it not…
user569322
8
votes
4 answers

why delegate must be static?

In code below I must declare method MdrResponseInterpreter static otherwise I have compilation error. class.... { private StandardBuilder _mdrResponseBuilder = new StandardBuilder(MdrResponseInterpreter); public static bool…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
8
votes
1 answer

When would a class ever have more than one designated initializer?

Reading through Apple's documentation on Tips and Techniques for Framework Developers, I came across this statement about designated initializers: A designated initializer is an init method of a class that invokes an init method of the…
CIFilter
  • 8,647
  • 4
  • 46
  • 66
8
votes
6 answers

Why doesn't initializer work with properties returning list?

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 …
tube-builder
  • 686
  • 1
  • 13
  • 29
8
votes
1 answer

Direct-init vs. list-init in array new-expression

Basically, why is this valid: auto p1 = new int[10]{5}; but this is not: auto p1 = new int[10](5); And more generally what are the rules for the new-expression initializer? I found the following: — If the new-initializer is omitted, the object is…
ledonter
  • 1,269
  • 9
  • 27
8
votes
1 answer

Finding static initializers and destructors in C++

I have a program with way too many static initializers and destructors. I want to get rid of all of them. So i need a way to find them. Running nm on the executable gives something like this: 0004bfc0 t…
8
votes
2 answers

Initializing a vector class member in C++

I'm trying to set length and initialize a vector member of a class, but it seems it's only possible if initializing line is out of class. //a vector, out of class set size to 5. initialized each value to Zero vector vec(5,0.0f);//its…
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
8
votes
2 answers

What does char c[2] = { [1] = 7 }; do?

I am reading Bruce Dawson's article on porting Chromium to VC 2015, and he encountered some C code that I don't understand. The code is: char c[2] = { [1] = 7 }; Bruce's only comment on it is: "I am not familiar with the array initialization syntax…
Moby Disk
  • 3,761
  • 1
  • 19
  • 38
8
votes
2 answers

Subclassing MKCircle in Swift

I'd like to subclass MKCircle (e.g. MyCircle) by adding another String property, let's call it "code". This property shall not be an optional and constant, so I have to set it from an initializer, right? Of course MyCircle should also get center…
8
votes
4 answers

Can I overload an implicit initialization to 0?

Is it possible to write a class such that these are valid: Foo a; Foo b = 0; Foo c = b; Foo d(0); Foo e(1); Foo f = Foo(1); But these are not: int x; Foo a = x; Foo b = 1; Foo c = 2; //etc Essentially, my rule is "A constant 0 is implicitly…
Eric
  • 95,302
  • 53
  • 242
  • 374
8
votes
5 answers

How to generate initializer in Ruby?

It's time to make it shorter: class Foo attr_accessor :a, :b, :c, :d, :e def initialize(a, b, c, d, e) @a = a @b = b @c = c @d = d @e = e end end We have 'attr_accessor' to generate getters and setters. Do we have…
Chris Xue
  • 2,317
  • 1
  • 25
  • 21