All Questions
Tagged with constructors object-oriented
32 questions
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()
{
...
-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 ...
-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/...
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 ...
9
votes
6
answers
3k
views
How do you decide if a parameter should go to the constructor of the method it relates to?
I use Python but I guess my question applies to OOP in general. Whenever I create a class I am never sure whether I should put a parameter/attribute in the constructor or in the method the parameter ...
8
votes
3
answers
1k
views
Avoiding side effects in immutable class constructor
I rewrote a very long method in which some data is queried from a database, based on info about a particular account, which is queried first.
I split out the account info into an immutable inner ...
1
vote
1
answer
354
views
ES6 - Where is it better to initialize a member - parent class or derived class?
I am trying to figure out if there is a better practice for initializing class members of derived classes in ES6 - in the child or the parent, and why?
For example:
Option 1:
class AbstractAnimal {...
1
vote
2
answers
333
views
Performing serial communication in constructor to initialize an object
I have a class UnitInfo which represents a collection of unit information with methods to get the unit information in a structured way, such as a specific encoding, etc. This unit info consists of ...
4
votes
2
answers
1k
views
Verifying Parameters in Constructor or Service
I was reading on this SO page about when to check parameters when constructing an object. The accepted answer suggests throwing the exception from the constructor so that an invalid object cannot be ...
1
vote
3
answers
779
views
Is calling .sort() in the constructor a violation of the guideline that a constructor shouldn't do work?
Suppose I have the following List to hold a list of fruits.
Example:
def fruits = ["Apple", "Orange", "Grapes"]
def fruitsBowl = ["Apple", "Grapes", "Orange"]
// Will print false
println(fruits....
2
votes
4
answers
872
views
What do OOP languages gain from having constructors that always return an object?
In what seems like a deliberate design decision, C++ does not have a null value for objects and references. This makes using objects and references very elegant since we don't have to perform null ...
3
votes
1
answer
391
views
Does adding values to a dictionary in the constructor violate that "a constructor shouldn't do work"?
Some what of a followup to this question.
Suppose I have a Player class with a dictionary call attributes that holds the Health and Mana of the player. In this example, I'll use Python, but my ...
1
vote
1
answer
352
views
Does creating a list in the constructor violate the guideline that a constructor shouldn't do work?
I was reading these pages (1,2,3), but I'm still unsure if this violates the guideline.
I have the following data being read from a website:
Date: July 13, 2018
Type: Partial Solar Eclipse
Location:...
0
votes
1
answer
588
views
Constructors or setters for entity classes? [duplicate]
I know this has been asked a lot of times but we are talking about entity classes here. Plain dumb objects containing nothing but primitives for properties. The purpose here is to store data. Our ...
1
vote
1
answer
515
views
Instance of other class within class, instead of constructor instance variables?
I was watching an OOP lesson with such code:
class UsersConstroller {
protected $userService
protected $logger
public function __construct {
UserService $userService,
...