Questions tagged [object-construction]

For questions related to object construction, usually in an OOP environment.

Construction is a concept of Object-Oriented languages.

Objects need to be setup for use:

  • Their member variables need to be allocated
  • Any Object member variables also need to be constructed
  • The Object methods need to be setup for calling

This process is accomplished by construction.

95 questions
264
votes
9 answers

Why should I prefer to use member initializer lists?

I'm partial to using member initializer lists for my constructors, but I've long since forgotten the reasons behind this. Do you use member initializer lists in your constructors? If so, why? If not, why not?
oz10
  • 153,307
  • 27
  • 93
  • 128
119
votes
5 answers

How can I initialize C++ class data members in the constructor?

I've got a class that has a couple of data members of class type. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I thought maybe I could do the following,…
Logical Fallacy
  • 3,017
  • 5
  • 25
  • 41
62
votes
5 answers

If two objects are declared in a single line, in which order are they constructed?

Let's say a class has been defined as class A { //..... }; and now I am creating two objects as A a,b; In what order are a and b created? Is it defined by the standard?
pasha
  • 2,035
  • 20
  • 34
35
votes
2 answers

JavaScript: using constructor without operator 'new'

Please help me to understand why the following code works: In the first line there is no new operator. As far as I know, a contructor in…
GetFree
  • 40,278
  • 18
  • 77
  • 104
34
votes
6 answers

How do I initialize classes with lots of fields in an elegant way?

In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like…
Patrick
  • 12,336
  • 15
  • 73
  • 115
14
votes
2 answers

std::initializer_list constructor

In code like this: #include #include #include struct A { A() { std::cout << "2" << std::endl; } A(int a) { std::cout << "0" << std::endl; } A(std::initializer_list s) { std::cout << "3" <<…
toozyfuzzy
  • 1,080
  • 1
  • 9
  • 20
12
votes
4 answers

How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?

I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream: public static final FilterFactory BZIP2_FACTORY = new FilterFactory() { public InputStream makeFilter(InputStream in) { // a lot of other…
Zack
  • 6,232
  • 8
  • 38
  • 68
11
votes
1 answer

What is the purpose for std::construct_at to cast through a pointer to volatile when using placement new?

According to cppreference, std::construct_at(T*p, Args&&... args) is equivalent to return ::new (const_cast(static_cast(p))) T(std::forward(args)...); What is the need/purpose for the cast 'through' const…
Walter
  • 44,150
  • 20
  • 113
  • 196
10
votes
6 answers

Is Java slow when creating Objects?

In my current project (OpenGL Voxel Engine) I have a serious issue when generating models. I have a very object oriented structure, meaning that even single parameters of my vertices are Objects. This way I am creating about 75000 Objects for 750…
th3falc0n
  • 1,389
  • 1
  • 12
  • 33
9
votes
7 answers

Does Java have a default copy constructor (like in C++)?

Does Java has a default copy constructor as C++? If it has one - does it remain usable if I declare another constructor (not a copy constructor) explicitly?
7
votes
1 answer

How do I construct an object in Perl6 from a hash?

In Perl5 you can do something like this: #!/usr/bin/env perl use 5.010; package Local::Class { use Moo; has [qw( x y )] => ( is => 'ro'); sub BUILDARGS { shift; return (@_) ? (@_ > 1) ? { @_ } : shift : {} } } use Local::Class; # Create…
jja
  • 2,058
  • 14
  • 27
7
votes
3 answers

Object construction/Forward function declaration ambiguity

Observation: the codes pasted below were tested only with GCC 4.4.1, and I'm only interested in them working with GCC. Hello, It wasn't for just a few times that I stumbled into an object construction statement that I didn't understand, and it was…
Gui Prá
  • 5,559
  • 4
  • 34
  • 59
6
votes
5 answers

How should I construct a subclass using an instance of the superclass?

Let's say I have the following code public class SuperClass { protected int super_class_value; public SuperClass (int value) { this.super_class_value = value; } } public class Subclass extends SuperClass { protected int…
Tester101
  • 8,042
  • 13
  • 55
  • 78
5
votes
1 answer

Why does object.__new__ accept parameters?

Besides the obvious asking "again" about __new__ and __init__ in Python - I can ensure, I know what it does. I'll demonstrate some strange and to my opinion undocumented behavior, for which I seek professional help :). Background I'm implementing…
Paebbels
  • 15,573
  • 13
  • 70
  • 139
5
votes
3 answers

How to make an object constructor in JavaScript;

Could you please explain me what's wrong in the following code? function box (width, height, color) { this.width=width; this.height=height; this.color=color; this.style.width=width; this.style.height=height; …
1
2 3 4 5 6 7