Questions tagged [constructor]

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

constructor
Filter by
Sorted by
Tagged with
0 votes
1 answer
37 views

Problem in Understanding Inner Constructor Method

I was trying to execute the following code: struct Pris length::Real width::Real height::Real function Pris() new(1,0,1) end function Pris(l::Integer,w::Integer,h::...
uzairphd's user avatar
1 vote
1 answer
30 views

The constructor does not exist

I am trying to make a simple class for a pizza topping that can be dragged around with the mouse. When trying to implement this class, I get the error "the constructor "Toppings" does ...
Marshall James's user avatar
4 votes
3 answers
205 views

Log a string when console.log(new X()) is executed

In interview, I was asked that any string for e.g. "Hello" should be consoled when below line of code is executed. console.log(new X()) This should be achieved without any additional ...
Murtuza's user avatar
  • 372
1 vote
0 answers
24 views

Mocking Singleton Class Constructor using Powemock

The problem is exactly similar to the one described How to mock enum singleton with jmockit?. I am using powermock with Junit in Java. there are some other singlton classes too without constructor ...
gansha9451's user avatar
1 vote
3 answers
104 views

Java constructor branching

I have a constructor which accepts an argument, which may be invalid; if it is invalid, I want it to run the constructor's no-argument constructor instead. Otherwise, it should proceed with the given ...
xland44's user avatar
  • 63
0 votes
2 answers
77 views

SystemVerilog constructor return value

I want to do something like this, but it results in an error: class MyPacket; function MyPacket not_fun_ction(); $display("not fun"); return this; endfunction endclass module ...
G. B.'s user avatar
  • 584
2 votes
1 answer
57 views

Overwriting default values in parent constructor in javascript/typescript

I have an abstract Typescript class with a constructor. This constructor accepts an object and then tries to set properties of the class with the data in the object. export default class AbstractModel ...
bleenders's user avatar
  • 920
2 votes
1 answer
168 views

If a destructor is deleted, will the compiler still implicitly generate a default constructor?

If a destructor is deleted, will the compiler still implicitly generate a default constructor? The following code can be compiled by GCC: struct A { ~A() = delete; int x; int y; ...
Yue Zhou's user avatar
0 votes
0 answers
24 views

Overloading constructor in annotation

Is it possible to overloaded constructor of annotation I'd like to have something to this: enum class Notes(val name: String, val comment: String) { A("A", "comment for A"), ...
Vik's user avatar
  • 1
0 votes
0 answers
55 views

Update the settings of a class after construction

I got the following structure: First, a config class: class Config: def __init__(self, config_path='./configs') -> None: file = open(config_path, 'r') self.config_path = ...
kklaw's user avatar
  • 495
2 votes
1 answer
53 views

Initializing a final field in a constructor using Class.this.field = value gives an error whereas using this.field = value doesn't

class A { final int a; A () { this.a = 1; // No error } } whereas the below code class A { final int a; A () { A.this.a = 1; // Error: java: cannot assign a value ...
ls642's user avatar
  • 25
0 votes
0 answers
54 views

What does `const&` mean in `const char* const& _str`? [duplicate]

This is a constructor of class MyString: MyString(const char* const& _str = nullptr); Inside it, we give const char * and then again const& and then _str, what does const& mean here?
Umair Ahmed's user avatar
3 votes
1 answer
107 views

Unexpected infinite recursion in C++ constructors [duplicate]

A minimal example is: #include <vector> class A { private: // where the real initialization happens void constructor_impl(int); void constructor_impl(const A&); public: A(...
AlumKal's user avatar
  • 33
0 votes
1 answer
58 views

ASP.NET CORE Redirect inside the constructor

I am developing a web project using asp.net core 7 and razor pages. I'm dealing with a routing issue. In the Controller, I check whether the user is logged into each method. I think this does not ...
OsKyy's user avatar
  • 55
0 votes
0 answers
46 views

C++ - Is it possible to use both types of constructor in an inherited class or when can I use second code type? [duplicate]

I have abstract class Father and inherited class Son. Father has attributes name and properties. Son has inherited name, properties and own school attribut. Father.h: #include <iostream> #...
Mirek's user avatar
  • 33
0 votes
0 answers
41 views

Choosing Between @Autowired and Constructor Injection in Spring: Which is Best Practice? [duplicate]

In my Spring project, I'm injecting dependencies into my services using both @Autowired and constructor injection. While both approaches work, I'm unsure which one is considered the best practice in ...
Aamna Zahid's user avatar
3 votes
2 answers
119 views

Lifetime issue instance of class || How to build my copy constructor?

My problem lies in the creation of instances of a class in my project. This is how I create my instances and try to store them: for (const auto & entry : fs::directory_iterator(path)) { ...
notfynnaf's user avatar
1 vote
2 answers
105 views

How to delegate to differnt constructor of a base class and call a derived class constructor?

I have this sample code: #include <iostream> class A { public: A() { std::cout << "Default constructor of A" << '\n'; } A(int i) { std::...
463035818_is_not_an_ai's user avatar
2 votes
2 answers
112 views

Constructor inheritance in C++. Derived class's default constructor is not being called

I have this sample code: #include <iostream> class A { public: A() { std::cout << "Default constructor of A" << '\n'; } A(int i) { std::...
Horace's user avatar
  • 153
1 vote
1 answer
94 views

JS Date constructor is inconsistent for future dates

I am doing some date manipulation in Office Scripts in Excel, and I noticed a strange behavior with dates future vs. past--I don't know if anyone else gets similar results because I can't consistently ...
Izek H's user avatar
  • 31
0 votes
0 answers
60 views

If I inherit some attributes from father's pure virtual class and want to implement them in main, do I have to redefine them in the inherited class?

If I inherit some attributes from my father's pure virtual class and want to implement them in main, do I have to redefine them in the inherited class? This is pure virtual class Father with ...
Mirek's user avatar
  • 33
0 votes
1 answer
51 views

Error when not creating default constructor [duplicate]

As a part of my project, I created a parameterized constructor and didn't create a default constructor. When I create an object of that class without passing parameters in 'main()' method and try to ...
Dev's user avatar
  • 1
0 votes
1 answer
143 views

Error extending class in Dart: "no_generative_constructors_in_superclass"

I'm encountering an issue in Dart related to class inheritance and constructors. The error message I'm receiving is: error: The class 'PathWithActionHistory' can't extend 'Path' because 'Path' only ...
S I D D H A R D H A's user avatar
0 votes
0 answers
46 views

Why does my code generate the "Debug Assertion Failed! _CrtIsValidHeapPointer(block)" error?

When trying to implement these two classes, calling the copy constructor and the = operator gives the "Debug Assertion Failed! _CrtIsValidHeapPointer(block)" error even though the console ...
Alex's user avatar
  • 1
0 votes
1 answer
50 views

Define __getitem__() wthin class constructor

Python allows assigning function definitions to class members directly like, class A: def __init__(self): self.test = lambda x: print(x) a = A() a.test(10) # prints 10 However for __getitem__(...
gordon_freeman's user avatar
-4 votes
1 answer
61 views

How can I obtain data from a server (HTTP POST) during class construction without putting the HTTP POST in the constructor itself? [closed]

I have a class that in order to have all its properties fully populated it needs to obtain some data from the server. Currently I have a call to the server in its constructor like so: public class ...
MonkeyFace's user avatar
-1 votes
1 answer
67 views

The constructor Game(String, int, int, double, String) is undefined

So I am making a store in java and for some reason when doing class inheritance it says my constructor is undefined. Below is my code public class Store { private String name; private ...
Andrew's user avatar
  • 1
-4 votes
1 answer
113 views

Why a Constructor in Java doesn’t have a return type? [duplicate]

I've read that constructors are automatically invoked when an object is created, but I'd like a more in-depth explanation regarding why they are different from regular methods in this aspect. Any ...
Kasun Jayasanka's user avatar
2 votes
3 answers
80 views

Pass Non-Parameters as Parameters to a base class

class Food { int Kg, Quality; public: Food(int kg, int quality) { Kg = kg; Quality = quality; } ~Food(void) {} }; class Pizza : public Food { public: int ...
lapdoggo's user avatar
-1 votes
2 answers
93 views

Outputting w different classes & Constructors

UPDATE: it was an issue that the file I was gathering text from had lol. Whenever I try running the output, there are multiple exceptions in everything past the welcome message that prevent any output ...
moemoe's user avatar
  • 1
1 vote
0 answers
27 views

Cannot call ViewPagerAdapter constructor in android

SO I came up with this issue having viewpager adpater constructor not being recognized in my main activity which is basically a service, extends InputMethodService Now the issue is it cannot recognize ...
P0tat0627's user avatar
0 votes
0 answers
23 views

TypeError: AngularCompilerPlugin is not a constructor WEBPACK issue

I am facing webpack issue after my Angular 11-15 upgrade.My upgrade went good. My build is failed. TypeError: AngularCompilerPlugin is not a constructor.My webpack.prod is failing need an expert ...
sanjib das's user avatar
0 votes
1 answer
41 views

How do I hand over lists to classes and work with a copy of that list inside an instance of the class

please see code example below: Main Program: List<MyObject> _listofmyobjects = new(); for (int i = 0; i <= 1000; i++) { _listofmyobjects.Add(new MyObject(i)); } var test = new Test(...
chrisk's user avatar
  • 27
-1 votes
2 answers
161 views

Could an implicit compiler created default constructor have more than a null body?

Could an implicit compiler created default constructor have more than a null body? According to IBM's website, the answer is: no. I have this project that's kind of stumping me though: This is where ...
Horace's user avatar
  • 153
0 votes
0 answers
42 views

passing a variable and its value from one class to another through inheritance c#

I want to use inheritance when calculating the area and volume of a cone. I have a circle class whose variables I want to use. I don’t understand why the area value is not transferred and calculated ...
Nenavizhu_Sumerki's user avatar
2 votes
1 answer
35 views

Java: how to let compiler show error when specific classes defined constructors which has any argument?

Sometimes, some classes will be only dynamically instantiated by Class.newInstance(), such classes should not define any constructor which has any argument because they will not be called anywhere. e....
osexp2000's user avatar
  • 3,040
0 votes
1 answer
84 views

OOP "Classes" and DOM manipulation with JavaScript, undefined element

This is my first personal project using JavaScript to make a website dynamic. I don't feel utterly lost, but after a few days I will take a knee at being "stuck". I have a grid container ...
softhearthe's user avatar
0 votes
1 answer
124 views

Initialize an array with another array in member initializer list [duplicate]

I am writing the constructor for class A below. mutex_array calls the default std::mutex constructor, which is fine. I then want to construct each element of unique_lock_array from each element of std:...
Bobby's user avatar
  • 73
0 votes
1 answer
31 views

Issue with Inheritance in Python - Unable to Access Parent Class Method

I'm currently working on a Python project where I'm using inheritance to create a base class and a derived class. However, I'm encountering an issue when trying to access a method from the parent ...
Divyansh Singh's user avatar
12 votes
2 answers
560 views

Why do C# 12 primary constructors execute in opposite order?

Why do primary constructors in C# 12 do execute in opposite order? That's kind of a breaking change to say the least... Example: namespace Whatever; [TestClass] public class UnitTestTemp { [...
Eric Cartman's user avatar
  • 16.2k
1 vote
2 answers
146 views

Controlling class member layout AND destructor order

I have a class with two members. class C { typeA a; typeB b; }; Assume that there is no inheritance. There are three things I'm concerned about with this class. the order in which the ...
Bobby's user avatar
  • 73
1 vote
1 answer
65 views

gcc/clang C++: move constructors not called but required in a code like A a, b; A c(a+b);

I have this code: #include <iostream> using namespace std; class A{ char a; public: A(): a('#') { cerr << "DEFAULT!" << endl; }; A(char a): a(a) { cerr << &...
Robert Szczelina's user avatar
-1 votes
1 answer
72 views

What's the reason to call/create non-default constructor instead of method? [duplicate]

Since I'm new in this field,it's better to learn good habits. I came across an article Java Error: The constructor is undefined where constructor undefined error appeared. All is clear to me, just ...
rado_rud's user avatar
12 votes
2 answers
303 views

Is there a good reason to split in two different cases the following ArrayList constructor in the OpenJDK code?

When reading through the OpenJDK code for class ArrayList, for instance, in JDK17 (https://github.com/openjdk/jdk17/blob/master/src/java.base/share/classes/java/util/ArrayList.java) I stumbled upon ...
Jesus Aransay's user avatar
0 votes
0 answers
34 views

C# default constructor even if there is an explicit constructor [duplicate]

I've defined a C# struct: public struct Separator { readonly StringBuilder sb; readonly char ch; bool more; public void Write() { if (more) sb.Append(ch); ...
rwallace's user avatar
  • 32.4k
3 votes
1 answer
3k views

Angular 17 constructor vs OnInit

Related to but not the same as Difference between Constructor and ngOnInit. In Angular 16 and 17 we can now use the takeUntilDestroyed operator and Signals. Both of these seems to work best in an ...
Bjarke Pjedsted's user avatar
0 votes
1 answer
55 views

how to fix All final variables must be initialized, but 'city' isn't

this my code final City city; CityCard(city, {super.key, dynamic id, String? name, String? imageUrl}); @override Widget build(BuildContext context) { return ClipRRect( borderRadius: ...
aldy waliyuddin's user avatar
0 votes
0 answers
39 views

Dynamically invoking super() with different parameters based on some conditions

I have a requirement where in I have a class in Angular that extends a Base class. Now, there is the feature Toggle Service that I am using to check for a feature. If it is toggled on, I invoke super ...
user22310316's user avatar
1 vote
0 answers
38 views

Java Classes - Why is one of these valid and one is not? [duplicate]

I'm kind of confused about how java classes work. How come when I do this, it gives me an error that says: "Non-static variable cannot be referenced from static context": public class Test { ...
user22996223's user avatar
0 votes
2 answers
45 views

How to access inherited constructor parameters in Kotlin

I'm in the process of working through a kotlin example project. One of the tasks is to implement a game board class that can track coordinates. The provided interfaces look like: interface SquareBoard ...
pbuchheit's user avatar
  • 1,495