Questions tagged [object-initialization]
56 questions
38
votes
5 answers
Setting properties via object initialization or not : Any difference ?
Here's a simple question : Is there any (performance) difference between this :
Person person = new Person()
{
Name = "Philippe",
Mail = "phil@phil.com",
};
and this
Person person = new Person();
person.Name = "Philippe";
person.Mail =…

Philippe Lavoie
- 2,583
- 5
- 25
- 39
36
votes
2 answers
Why is a POD in a struct zero-initialized by an implicit constructor when creating an object in the heap or a temporary object in the stack?
The standard and the C++ book say that the default constructor for class type members is called by the implicit generated default constructor, but built-in types are not initialized. However, in this test program I get unexpected results when…

Jacobo de Vera
- 1,863
- 1
- 16
- 20
7
votes
1 answer
Is initializer evaluated after memory allocation in new expression?
Consider the code
auto p = new T( U(std::move(v)) );
The initializer is then U(std::move(v)). Let's assume that T( U(std::move(v)) ) does not throw. If the initializer is evaluated after the underlying memory allocation, the code is then…

Lingxi
- 14,579
- 2
- 37
- 93
6
votes
2 answers
Python Module Initialization
Is it bad practice to initialize the objects in the module, in the module code?
in Module.py:
class _Foo(object):
def __init__(self):
self.x = 'Foo'
Foo = _Foo()
Than in user code, you could:
>>> from Module import Foo
>>> print…

tMC
- 18,105
- 14
- 62
- 98
6
votes
6 answers
What is the difference in how these objects are instantiated?
What is the difference between the two cases below:
class Data
{
PersonDataContext persons = new PersonDataContext();
public Data() {}
}
versus
class Data
{
PersonDataContext persons;
public Data()
{
persons = new…

Tuyen
- 83
- 7
5
votes
1 answer
Java Double Object initialization with other Number type objects
In Double object documentation it only have two constructors, one taking a double value and one taking a string value. However, I just found that if we initialize it with other Number type objects it will also work. For example the following code…

sabcyang
- 53
- 4
5
votes
2 answers
Nested transaction scopes constructed with object initialization results in error
In my C# code I am using nested transaction scopes. I have a utility class that creates TransactionScope objects identically. Both the outer scope and the inner scope are constructed in exactly the same way.
If I construct the TransactionScope…

Doodly Studmuffin
- 91
- 1
- 5
4
votes
5 answers
Need explanation on this Java object initialization syntax
I'm a C\C++ programmer just starting on Java.
I came across this working Java snippet syntax that I understand what it does but I can't understand the logic of the syntax.
object x = new object
.SetContent(aaa)
.SetIcon(bbb)
…

aRIEL
- 167
- 1
- 3
3
votes
3 answers
Object as a parameter of itself in lisp
In Python, I would do like this:
class foo:
def __init__(self):
self.x = self
Otherwise, now the object is a parameter of itself. How can I do it in common lisp?
(defclass mn ()
((pai :accessor mn-pai
:initarg :pai
…

Matheus Popst de Campos
- 139
- 7
3
votes
1 answer
Scala: Overridden value parent code is run but value is not assigned at parent
Running the code below:
class Parent {
val value = {
println("Setting value in parent")
"ParentVal"
}
println(s"Parent value is ${value}")
}
class Child extends Parent {
override val value = {
println("Setting value in child")
…

PetrosP
- 635
- 6
- 15
3
votes
2 answers
Create list of items of a single item at once
Angular task 1.5.x:
I have object like this:
{
id: 1,
name: "a",
items: [
{"holiday": "false", "day": "monday"},
{"holiday": "true", "day": "tuesday"...}
]
}
I want a new object to be created in the above way with…

user2349115
- 1,288
- 2
- 17
- 34
2
votes
0 answers
How to initialize object attributes from an array of strings in Python?
I am supposed to write a class which will allow the user of the script to define what attributes will it's objects have when initializing them by giving it an array of strings like this.
data = Database(tables=['distance', 'speed'])
Then it should…

FallingBrick
- 21
- 2
2
votes
4 answers
How can I initialize an array of objects?
I've written this code but I have some errors when I try to initialize an array of Critter objects and don't know what they're about.
My code:
#include
#include
#include
using namespace std;
class Critter {
private:
…

myregm
- 67
- 1
- 8
2
votes
2 answers
C# What is the Use of Square Brackets ([]) when construction an Object in C#
the Question may be very basic, but I stumbeled across a line of Code I have never seen and was wondering what the use of the Square Brackets are.
public NodeItem (bool isWall, Vector2 pos, int x, int y)
{
this.isWall =…

user3086972
- 85
- 7
2
votes
1 answer
Assertions in abstract superclass scala creating NPE
The following code, when entered in REPL
abstract class A { val aSet: Set[Int]; require(aSet.contains(3)) }
class B extends A { val aSet = Set(4,5,6) }
new B()
gives a null point exception, rather than an invariant failure.
What would be the best…

A. D
- 35
- 4