21,178 questions
-7
votes
0
answers
54
views
Difference between object initialization methods? [duplicate]
What's the difference between initializing an object by passing in variables to the constructor or through the brackets? What would be the proper use case of either method?
public class MyClass()
{
...
21
votes
1
answer
1k
views
Heisenbug where static_assert is satisfied but triggers other static_assert
I have pinned a strange compilation error to this minimal code example:
#include <iterator>
#include <type_traits>
struct A {
class iterator {
private:
int i = 0; // compiles if &...
-3
votes
1
answer
323
views
How do you make a default constructor and fix the type deduction error indicated by E3158 and E0289?
I am making several template classes and their non-initialized declarations give errors E3158 and E0289, indicating that my compiler cannot identify the class's template arguments.
What is a way ...
2
votes
0
answers
76
views
R6 object aware of its constructor
I was wondering if it's possible to get the object constructor from within an R6 object. One obvious way would be something like this:
test <- R6::R6Class('test',
public = ...
Best practices
0
votes
8
replies
53
views
Initialize a Typescript class property once
Let's say I have a class with many parameters. In Typescript, this is how the class is created:
class ClassName {
private para1: string
para2: boolean
para3: number = 6
protected ...
3
votes
1
answer
116
views
Powershell Class generic interface arguments type resolution problem
Can't seems to find why generic interface types are allowed in powershell class argument attributes, but they can't accept anything - powershell class type resolution doesn't work in this case. Is ...
1
vote
2
answers
97
views
JavaScript: How many objects are allocated when returning an object literal from a class constructor?
Let's say I have this code:
class MyClass {
constructor() {
return {};
}
}
const x = new MyClass(); // how many objects are instantiated with this?
I know that variable x just holds ...
0
votes
0
answers
127
views
What makes QGuiApplication construction hang in this case?
Here's a piece of Qt code:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QGuiApplication app2(argc, argv);
The first constructor call returns, the second does not.
I ...
-5
votes
3
answers
224
views
std::shared_ptr const vs non-const, instance vs reference in constructor
Consider passing a shared pointer of const std::shared_ptr<Object> to the ctor of following class.
struct MyClass {
explicit MyCLass(const std::shared_ptr<const Object> & input) : ...
2
votes
2
answers
137
views
Subtlety in initializing attributes with methods in modules from the `equinox` `jax` library
I have the following code that defines an abstract class and its final subclasse. The two classes are both subclasses of the equinox.Module class, which registers class attributes as the leaves of a ...
3
votes
6
answers
220
views
Generalize a self referential struct construction using a variadic template
I have some tricky code that I need to initialize many objects that refer to each other. I want to allocate and construct them all at once, so I put them all in a struct.
It looks like this:
// User ...
1
vote
1
answer
98
views
In PHP, should a child class redeclare constructor dependencies if the parent constructor already defines them, or can it omit them entirely?
So I’m kinda stuck wrapping my head around this whole parent-child dependency thing. My assumption was, once you declare all the dependencies in the parent (BaseController), the child (like ...
-2
votes
4
answers
173
views
How can I automatically inject common services into all command/query handlers without manually modifying each constructor?
I'm working on a .NET application where every command/query handler needs access to three shared services: IValidator, ISecurity and ILogger. Rather than manually injecting these into each handler ...
4
votes
1
answer
76
views
__new__ function behaves differently after overriding it with setattr
I have the following:
class A:
def __init__(self, x):
self.x = x
A(5) # Works fine
x = A.__new__ # Save the function
A.__new__ = lambda y: y # Temporarily override it
A.__new__ = x # ...
1
vote
2
answers
118
views
Creating virtual object in constructor initialization
I want to create a variable of type Foo. The class Foo consists of a variable called Bar bar_ which is filled directly in the constructor initialization. The thing is, class Bar has a reference to the ...