Skip to main content

Questions tagged [constructors]

Member functions of a class which initializes objects of the given class.

3 votes
4 answers
348 views

Primary constructors for depedency injection

Microsoft identifies dependency injection as one of the uses for primary constructors. public class ExampleController(IService service) : ControllerBase { [HttpGet] public ActionResult<...
Jannick Breunis's user avatar
4 votes
3 answers
253 views

How do you handle instantiating a large number of interconnected component objects?

I'm currently refactoring our instance startup code, which is currently a 2,000 line mud method. The issue is that that method needs to create a large number (>50) of components/service objects, ...
thecoop's user avatar
  • 539
0 votes
1 answer
150 views

Assigning to fields in function or by function?

While writing the constructor for class A in Python, I calculate some of the class's fields using function fun(). This function is never used outside the constructor of instances of this class. In ...
Harm van den Brand's user avatar
2 votes
3 answers
860 views

Why do we need factories in the first place?

I went through various blogs but nowhere I can find the reason of having virtual constructors in C++. Why is virtual constructor needed and lets say if we go through the wrong way of creating virtual ...
H Kumar's user avatar
  • 31
3 votes
3 answers
471 views

How to efficiently build objects without default constructors

I am trying to explain and understand a way to create object which cannot have "default" instances. Typically I want classes which represent, as closely as possible, the real-life concept I ...
Patrick Wright's user avatar
-1 votes
1 answer
2k views

How are parameters values passed into a MVC controller constructor?

For this MS sample code, I have checked Program.cs, Startup.cs, and other config files, don't see how the two parameters sqlQueryService and sqlCommandService are populated. Appreciate if someone can ...
Jeb50's user avatar
  • 101
-2 votes
3 answers
225 views

How to signal a dealbreaker error from a c-tor?

Context: 128kB RAM, freeRTOS. Considered solutions: Exceptions. Discouraged by both the memory size and the code style guide. Late bool init(...);. Has worked for a decade but has it's problems - can ...
Vorac's user avatar
  • 7,159
-1 votes
1 answer
123 views

Smart pointer class choice: Simplicity vs. right tool for the job

I'm wrestling with a design choice question here. I've got a class that needs a couple of semaphores. Semaphores are non-movable objects. Objects of this class however need to go into a vector (there'...
T.E.D.'s user avatar
  • 1,069
2 votes
5 answers
573 views

Do db calls in constructors lead to more DRY code?

It recently came to my attention that its best practice to avoid database calls in constructors. I feel like this means you end up repeating unnecessary code, thus the code is less DRY? For example, ...
pooteeweet's user avatar
1 vote
1 answer
148 views

Constructor initializer list too bloated?

I noticed that my Member initializer list is not really readable. My main.cpp files looks like this: #include "Application.hpp" using Pathfinding::Core::Application; int main() { ...
a a's user avatar
  • 261
-1 votes
1 answer
197 views

Conversion methods: from_xxx() or to_xxx(), is there a reason I shouldn't stick with to_xxx()?

I have a C++ library that I'm converting to Python. In the C++ library I have multiple constructors and many different types of conversion functions (think radians to degrees, and different types of ...
Krupip's user avatar
  • 1,320
-2 votes
1 answer
300 views

Why aren't constructors atomic? [closed]

If thrown exceptions in constructors can lead to memory leaks or partially-constructed objects, then why we don't make them atomic? so neither an object nor it's local variables will get created/...
Rain's user avatar
  • 99
1 vote
2 answers
453 views

Can and should constructors do more work than merely assigning values to fields? [duplicate]

A typical introductory example to OOP, classes, and constructors is object Car, with properties such as float fuel, bool is_engine_running, etc etc, and a class and constructor definition might be as ...
SRSR333's user avatar
  • 335
0 votes
1 answer
234 views

Is a "wrong" to return anything else besides `this` in a constructor?

Is a "wrong" to return anything else besides this in a constructor? Take this TypeScript Queue sample: class Queue { private buffer: string; constructor(buffer: string = "&...
Daniel Santos's user avatar
-1 votes
3 answers
631 views

When and how to check input parameters

My SmsRecipientDetails class constructor accepts String value as recipient phone number. I would like to accept number with spaces: assertDoesNotThrow(() -> new SmsRecipientDetails("123456789 &...
jnemecz's user avatar
  • 299

15 30 50 per page
1
2 3 4 5
11