Questions tagged [constructor]

A special type of subroutine called at the creation of an object.

Filter by
Sorted by
Tagged with
6 votes
2 answers
445 views

Classes representing 2D points and pixels

How can I improve this code or make it tidier? ...
0 votes
0 answers
24 views

Constructor for a python class, receiving input from user or file, which happens to be for a Sudoku solver

There are quite a lot of Python Sudoku solvers on this site, but my question is more about style , structure, design and (if I can coin a phrase) Pythonicity (ie is it Pythonic) rather than ...
1 vote
1 answer
59 views

Tiny Validity class

Here is a part of my new code replacing the old and deprecated old-school one. Let's get to business right away: I document everything with Documentation comments, which are proving very useful so ...
5 votes
3 answers
76 views

Performing complex operation before calling the primary constructor in Kotlin

Semester is a simple enum class. ...
0 votes
1 answer
102 views

GameObject - Component hierarchy, component can access game object in constructor

While creating a game engine using c++, when defining a component, I was tired of writing down essential but repeated elements such as 'GameObject* parent' every time I define a new component ...
2 votes
2 answers
72 views

Setting instance variable for salary bonus based on academic position

I have a class Scholar which is basically a teacher in a university. I have to calculate the yearly salary of a scholar instance where the formula is ...
  • 33
-1 votes
1 answer
67 views

If constructor has actions besides assignment should I move those actions in separate method?

I have class CellGroup that contains List of cell lists That class has creation of list inside and transforming it to list of lists. ...
0 votes
2 answers
78 views

Made a monster array for a text based game with a class constructor C++ [closed]

I created a class in a separate .h and .cpp file with the idea of having an array of 3 monsters with each monster having different stats( name; health; damage). The problem is that the constructor ...
  • 9
4 votes
1 answer
104 views

Commands Object creation using chained Builder pattern

Context As many of you may know I have a library that allows C++ objects to be converted into JSON/YAML/BSON automatically with a single declaration (see previous code reviews). I am now (trying) ...
  • 91.9k
2 votes
1 answer
45 views

Modify parameter data before creating an instance of a class in PHP

I have an abstract Badge class, every class extending this class should always correctly set the incoming name (string) and achievedAt (date) fields. There might be other classes that extend the Badge ...
0 votes
2 answers
71 views

Set id in constructor, or generate it when asked for?

I'm using PHP, I have a class that generates an id based on the calling class, so the id of the class won't change after it has been initialised, now I'm wondering which of my two methods is ...
4 votes
1 answer
448 views

C++ wrapper class for array of OpenGL buffer objects -- just the constructors

A similar question was asked here, and I'm trying to do the same thing. But I'm trying a different approach by deriving from std::array, and This is a very focused ...
-1 votes
1 answer
322 views

Is performing IO in constructor good idea? [closed]

I have Java (or C#, Python, Kotlin, or other similar language) class that will be used to communicate with client over network. Protocol used by this application allows many different ways to create ...
  • 159
4 votes
1 answer
170 views

Randomizing all class variables in constructor c++

I'm playing around with making random items and decided to implement the following structure. ...
1 vote
1 answer
47 views

Passing argument to a constructor and using it only in some class methods

I've created some controller for handling clicked links statistics. Does this class meet Single Responsibility Principle? ...
0 votes
2 answers
503 views

Array indexing with copy constructor, assignment operator and index operator overload

The code below is a custom array class that handles indexing, copying, printing, etc. explicitly. Is there a better approach for specification (declaration and definition) of the assignment operator (...
1 vote
2 answers
148 views

Particle Swarm Optimization algorithm

This is part of a Particle Swarm Optimization algorithm, a snippet of a Particle class implementation. ...
  • 455
0 votes
3 answers
100 views

Minecraft city class [closed]

This is a class representing a city in a mod I am creating for Minecraft. All the constructor parameters are mandatory and they are used to help the class that calculates and builds the Paths, ...
  • 139
2 votes
0 answers
111 views

Convenient way to make an UIImageView ExpressibleByStringLiteral

I want to reduce boiler-plate code and write like this when developing in iOS: let imgView: UIImageView = "share_fareware_text_bg" Here is implementation code: <...
2 votes
1 answer
411 views

Constructor function for persons with hobbies

I'm trying to write a constructor function that generates instances of a user. One of the properties of the user class is an array of the user's hobbies. I'd like ...
  • 91
1 vote
1 answer
34 views

Adding Length dimension to a Crate Object

Consider Crate::addLength() in the following code: ...
  • 273
4 votes
1 answer
43 views

Time interval class that enhances constructor argument handling of its parent class

I am making a subclass of a well-established python class: pendulum.Period, which overrides both __new__ and ...
4 votes
1 answer
110 views

Super fancy list constructing in Python

Out of curiosity I was discovering the potential of the Ellipsis object in Python (...), and I discovered it doesn't do a whole ...
  • 445
2 votes
1 answer
53 views

Constructor for a packagetarget struct

As many of you know goto is usually signs of code smell. However I thought this could be an appropriate case, and would like confirmation or criticism. Unnecessary ...
4 votes
2 answers
2k views

Implementing a c++ Kafka producer based on librdkafka

To practice my c++ I decided to implement a simple Kafka producer to wrap the producer in c in librdkafka library. I just want to get your opinion on the way I have implemented teh default, copy and ...
  • 269
3 votes
2 answers
183 views

Constructing a cumulative distribution function from a histogram

During some running process, a histogram of values shall be build up. When done, the CDF shall be derived from it and used to get the quantiles for some values. My current implementation looks as ...
0 votes
3 answers
73 views

Represent a Class which holds translation Strings [closed]

I have a class which represents some translations of a keyword. ...
  • 141
5 votes
2 answers
367 views

C++ wrapper class to mimic a C array's brace initialization

I have an inheritance class that mimics the behavior of C style array's brace initialization by using a class template that has a variadic constructor but the template itself is not a variadic ...
3 votes
1 answer
579 views

C++ - Astar search algorithm using user defined class

The objective of the post is to improve design and improve command on C++. Given a map, start point and end point, the shortest path to end point from the start point has to be found out using Astar (...
  • 539
3 votes
1 answer
290 views

Load and update XML inside the constructor

My requirement is, I need to load the existing xml file from the given path by passing through command line arguments and need to update the xml file from C# code. First time I am working on the C# ...
  • 31
0 votes
2 answers
838 views

Fetching data from your database in a constructor

I have this item (an item is an object linked to a xml view), to build it I need 2 things, get labels from codes, and get text from the strings.xml android file. please ignore the long to int and int ...
-1 votes
2 answers
2k views

Constructors with variable number of arguments [closed]

Assume that we have a class with multiple constructors which may look like the following: ...
-1 votes
2 answers
88 views

Healthy Characters

Let's say I have an abstract class Character that have fields health and maxHealth. maxHealth...
3 votes
1 answer
93 views

Representing destinations in a square

I've written a program following the instructions of an exam paper. There are no solutions available so I would appreciate a feedback. I'm concerned with main in ...
  • 351
4 votes
1 answer
163 views

Designing the constructor interface for a reflection object (any class)

I am working on cleaning up and making open source a C++ reflection library that has served me well over the last few years. One of the most important classes of the library is ...
  • 143
1 vote
4 answers
172 views

Java Date class- switches and ifs in the constructor

I'm a beginner i have an assignment to write a Date class (as part of a bigger project). in my question i focus on the constructor. here's some background: the given guidelines are that the date is ...
1 vote
2 answers
654 views

Class for course information

Here's what the question asks: Build a class called Course.java. This class should have 4 properties: CourseId, CourseName, Description, and credit hours. Also add the appropriate set and get ...
  • 11
2 votes
1 answer
5k views

Testing async method call from constructor

I have a project where I want to build a more sophisticated ToDo list - basically a personal project management system. I'm just starting out with the project, and I'd like some feedback on whether my ...
  • 139
3 votes
1 answer
1k views

Character class with name, gender, and inventory list

I am trying to create a class to test my understanding, and it takes an ArrayList as parameter. The decision of choosing an ...
3 votes
4 answers
127 views

Simple addition calculator accepting addition expressions

Maybe someone can help me to optimize the calculate method. I think that it can be simplified from that what I have now. The objective is: grab a string via the ...
0 votes
2 answers
199 views

Ensuring a non-zero radius when constructing a circle

Is there a little "better" way of writing the line: raise RuntimeError if not radius.is_a? Numeric or radius == 0 I want to only allow numbers bigger than 0. <...
2 votes
2 answers
768 views

Elegant way to "construct" kind-of enum values in go

I'm quite new to Go and would like to improve my code. I have a representation for access levels in Go like this: ...
5 votes
2 answers
330 views

Request dispatcher for PHP JSON web service

Below you can find the constructor method and related fields of the main class of my REST API (note that the class contains more code than displayed) written in PHP 7.1. It does various things with ...
  • 207
0 votes
1 answer
2k views

Initializing values of a singly linked list with a constructor [closed]

I have to create a constructor that allows a new linked list to be populated with ten consecutive values, starting at 0. Then I need to print the list! So i want to check if the functions I wrote for ...
0 votes
1 answer
159 views

Controller with too many parameters with DI [closed]

Is there a way to reduce the number of parameters of this constructor? All the services must be used in this Controller. The majority of them are used more than 2 times(some of them >10), with the ...
3 votes
3 answers
4k views

OOP modelling of a car agency

Exercise: A car dealer wants a computer system to manage the data of their vehicles and classify them by type. All cars have the following data Engine serial number Brand Year ...
2 votes
1 answer
2k views

Factory using Structuremap to inject concrete implementation of BaseTask<T>

The problem statement that I am trying to solve is this; We will retrieve a list of Tasks from an external repository. These tasks will have a type and for each type, there needs to be a specific ...
  • 121
5 votes
1 answer
1k views

Simple c++ triple class to mimic pair for use with uniform initialization

I'm trying to create a simple triplet class, but the more I look at the implementation of std::pair the more I feel like I'm missing important details. What I have ...
  • 205
5 votes
1 answer
3k views

Counting the number of unique fractions read from a text file

I will begin this question with the disclaimer that it is homework but my code is written and working, I just want to know if there any ways in which I can improve what I already have. I want my ...
  • 73
2 votes
3 answers
117 views

Message class, with constructors for data from user and from database

I'm fairly new to Kotlin, and am looking to slim down a data class. It has two constructors, one for creation based on user input, and one for creation based on database data. ...