21,186 questions
3
votes
1
answer
100
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
89
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 ...
1
vote
0
answers
120
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
217
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
123
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
210
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
93
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 ...
-1
votes
0
answers
35
views
Why Symbols and BigInt can't be instancied using "new" keyword? [duplicate]
While I was learning the language, I became interested in primitive types and their object wrappers.
While I was in my code editor discovering this language feature, I discovered that any of these ...
-2
votes
4
answers
152
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
74
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 ...
6
votes
1
answer
250
views
Why can my C++ class be implicitly converted in one case, but not another?
I'm getting a compiler error about a type conversion that should be legal, to my eyes.
Let's say I have a home-grown string class, convertible from and to char*:
class custom_string
{
public:
...
1
vote
1
answer
89
views
PHP calling model function
I'm trying to learn constructors and statics and I'm not sure what I'm doing wrong here
controller
<?php
namespace App\Http\Controllers\Api;
use App\Models\SampleModel;
class SampleController
{
...
2
votes
1
answer
48
views
x class is not a constructor [closed]
My error is SiteChecker is not a function
I was following this guy steps https://www.thepolyglotdeveloper.com/2020/06/scan-broken-links-website-javascript/ to set up package and execute , but on my ...
0
votes
1
answer
123
views
How to explicitly instantiate forwarding constructor in C++?
I'm trying to explicitly instantiate a forwarding constructor using C++20 with GCC 13.3 compiler in order not to put its definition in the header file. Here is a minimal example:
Foo.h
#pragma once
...
2
votes
1
answer
144
views
Why is a field not initialized when using a custom record copy constructor?
I have this record:
record Foo {
string _bar = "initialized";
public string Bar => _bar;
}
I'm running the following code:
var foo = new Foo();
Console.WriteLine(foo?.Bar ?? &...
1
vote
1
answer
69
views
SML pattern matching on datatypes with constructors from function arguments
In the example, there is a "match redundant" Error, indicating that SOME s is matching on every string and not the provided s:
fun matchs (s : string) : (string option -> bool) =
fn x =&...
0
votes
1
answer
101
views
dart issue trying to initialize a Uint8List variable in a named constructor
import 'dart:typed_data';
class Test {
Uint8List testKey;
Test({this.testKey = Uint8List(0)});
}
Gives compile error: Error: Cannot invoke a non-'const' factory where a const expression is ...
0
votes
1
answer
51
views
Inheritance with multiple constructors and type annotations [duplicate]
I'm trying to create classes that can be saved to a JSON file. The idea is to convert my object to a dataclass that stores only the important attributes. I'll then use something from json or ...
3
votes
2
answers
84
views
Substituting template parameter with its known value into a typedef is inconsistent between compilers
The code is not an ideal implementation, but it demonstrates something interesting.
I have a class template with some bool parameters, where specializations of the class have slightly different ...
0
votes
1
answer
54
views
Why does referencing a this.function() in JavaScript not refer to the prototype element?
the below outputs true, meaning that new memory is not allocated for different objects, but they point to the same prototype Noob.
function Noob(name, game) {
this.name = name;
this....
0
votes
1
answer
150
views
Member "..." is not a typename, Constructor-definition (in .cpp) doesnt take struct as input variable
I'm trying to write some UI-Code (for learning purposes) and I get this error that I can't find a fix to.
The 4 errors are
`E0493` No instance of overloaded function "UIElement::UIElement" ...
6
votes
1
answer
241
views
Can I trust the Delphi default() to initialize string in a record to be empty?
Given this declaration:
type
TRec = record
FNr: integer;
FName: string;
constructor Create(ANr: integer);
end;
and this implementation of the constructor:
constructor TRec.Create(ANr: ...
1
vote
1
answer
70
views
Why does __init__ requires an explicit self as an argument when calling it as base.__init__()?
I know of two ways to call a superclass's constructor from a derived class: super().__init__() and base.__init__(self). Why does the second require me to explicitly supply self as an argument?
Here's ...
2
votes
3
answers
186
views
Initialization of base class members in a derived class constructor
In C++ inheritance, I understood that subclass's constructor cannot directly initialize the members of the parent class.
If it is so, why doesn't the code below cause an error?
class Person
{
...
2
votes
3
answers
127
views
Initialization of class array in a template class
I have a class which uses a fixed number of member classes (mInpA, mInpA in my case)
class csAndGate : public csLogicCell
{
public:
csAndGate() :
mInpA(this),
mInpB(this)
{}
...
0
votes
1
answer
145
views
Resetting data members by calling default constructor within a `reset()` member function [closed]
Consider a class where I'm using brace initialization for data members to provide default values at declaration.
If I also want to provide a reset() function, is there anything wrong with calling the ...
11
votes
1
answer
206
views
What constructors must be defined to satisfy copy constructible concept for any-like class?
I would like to make a class that satisfies std::copy_constructible concept and in addition can be constructed from an arbitrary copy constructible value much like std::any does:
#include <concepts&...
1
vote
1
answer
65
views
Validation when one constructor calls another with enum in C#
A simple class with a copy constructor, which calls another constructor:
public class Foo<TEnum> where TEnum : Enum
{
public Foo(TEnum myEnum)
{
MyEnum = myEnum;
}
public Foo(Foo<...
7
votes
2
answers
223
views
std::tie in constructor's initializer list
I have a following header:
#include <string_view>
#include <tuple>
std::tuple<int, int, int> foo(std::string_view sv);
class s {
public:
s(std::string_view sv);
...
0
votes
3
answers
141
views
How do I give the possibility to call multiple optional methods onto one parameter in a constructor in JAVA
I would like to make a constructor with an optional parameter, to avoid giving in Strings all the time, because I need to use the constructor like a thousand times, so I would like to call methods ...
-3
votes
2
answers
89
views
C# Run and wait an async void event in constructor
I'm writing a WPF app and am trying to test its click events quickly. As a first attempt, I called the click events in the constructor. These functions are async void so I wrap them with Task.Run()....
4
votes
1
answer
106
views
C++14: Inheriting constructors with "using" AND defining new constructors yields C2512 (no appropriate default constructor available)
In C++, you can inherit all constructors of a base class by writing using Baseclass::Baseclass
My Baseclass has both a default constructor and another one that takes an int argument, even though that ...
3
votes
1
answer
132
views
Do objects part of a class not get constructed until its constructor is called
For this simple classes below. For MyClass, the constructor initializes str with s during construction using the intializer list.
Does this mean that str doesn't get constructed at all yet until mc ...
1
vote
0
answers
75
views
In Scala 2/3, how to consistently determine when a package object or top level variable will be constructed?
Here is a quick experiment to demonstrate an inconsistent behaviour:
trait Foundation {
import Foundation.*
{
initialisationCounter.incrementAndGet()
}
case class Top1() {
}
}
object ...
-1
votes
1
answer
119
views
Does destructor take care of popping member variables from the stack
Take this very simple class. The main() function just creates an object of it. Even though the constructor and destructor don't have an implementation here. My assumption is that the constructor will ...
0
votes
0
answers
73
views
Are pointers to destructors pushed onto the stack. C++ [duplicate]
I am thinking about stack unwinding when an exception is thrown here. In the below simple example, when throw 505 is executed, from what I understand, the stack starts to unwind until it finds the ...
1
vote
3
answers
116
views
Calling parameterized constructor only to update one member variable
I have a class with a default constructor which initializes many member variables via initializer lists.
I wish to have a parametrized constructor which only updates one of the member variables. A ...
0
votes
2
answers
165
views
How to make an async call in a constructor in c#? [duplicate]
I would like to create a class in C#, where some of the property need to be set after a call to a database.
A a crude example example:
I want to create a shipping class for some furniture objects.
In ...
0
votes
2
answers
46
views
Python subclass constructor calls metaclass with *args and **kwargs
I am trying to instantiate a singleton with constructor argument. However, passing the input arguments to the metaclass hits an error.
from typing_extensions import List, TypedDict, Optional, Any
...
1
vote
0
answers
86
views
Passing a reference to a constructor causes runtime exception [duplicate]
I refactored some code and now my unit test is crashing. The code being tested runs fine in the project, but when I inherit from the class in the test fixture there is an error when running the ...
0
votes
0
answers
101
views
Why doesn't cause every thread the constructor to run when the class is initialised with a thread_local?
I have a thread_local variable, which should initialize a class in every thread, but the constructor isn't called, when the threads are opened. The constructor is only called, when I place the ...
1
vote
2
answers
321
views
Avoiding null attributes in records
I have the following record. But the collection cannot be null. Is there a way to default it to an empty list if it's null?
public record Car(
long id,
String model,
List<...
-1
votes
5
answers
160
views
Class level instantiation vs constructor or init block [closed]
Probably like many devs, I've picked up practices etc. based on the existing code as well as conversations with colleagues. A question just occurred to me, and I'm not sure if this is a 'best practice'...
2
votes
1
answer
122
views
Selective constructors in C++
I'm new to C++ and I have a task:
create a class that represents a point on a 2D coordinate plane and then create 3 constructors: one that will define both coordinates and a name (consisting of a ...
2
votes
3
answers
151
views
C++ How to define a member variable that is either passed into or constructed within a constructor
What is a way to define a member object variable of a class that can either be
passed into the constructor (ownership stays external to class) or
constructed inside the class's constructor (ownership ...
1
vote
1
answer
260
views
Angular 19 Fire cls is not a constructor
I have an Angular Firebase app with
"@angular/cdk": "19.0.4",
"@angular/common": "19.0.5",
"@angular/compiler": "19.0.5",
"@angular/...
0
votes
1
answer
78
views
Passing a constructer as a parameter as if it were a regular function in JavaScript?
I am using ANTLR 4, and it is common to instantiate a class and immediately pass it as the argument of the next class' constructor, as per the docs for the ANTLR 4 JavaScript target:
import antlr4 ...
0
votes
2
answers
211
views
java: variable employeeService not initialized in the default constructor
I create a new project in java springboot. When I try to run the code it gives the following error.
D:\Project\New folder\demo\src\main\java\com\example\demo\controller\EmployeeController.java:15:35
...
1
vote
1
answer
88
views
Constructor for Multi-level inheritance in C++
Please see following C++ code:
#include <iostream>
using namespace std;
class alpha
{
int a;
public:
alpha () {};
alpha (int c)
{
a = c;
}
void showalpha (...