Skip to main content
Filter by
Sorted by
Tagged with
3 votes
2 answers
201 views

Can I dynamically generate a contiguous sequence of non-default-ctorable objects which can be deleted with delete[]?

Suppose I have: Identifier Meaning T a non-default-constructible class gen a function returning a T n a positive size_t value Now, I want to generate n T-objects, using gen, consecutively on the ...
einpoklum's user avatar
  • 130k
3 votes
1 answer
102 views

explicit deleted constructors - does this matter?

When marking a constructor as deleted, i.e. class MyClass { // ... MyClass(int x) = delete; // ... }; is there any effect to marking the deleted constructor as explicit? i.e. ...
einpoklum's user avatar
  • 130k
13 votes
3 answers
2k views

Is it legal to initialize an array via a functor which takes the array itself as a parameter by reference?

In the question Idiom for initializing an std::array using a generator function taking the index?,which basically asks how one can initialize an array of an arbitrary type which is not necessarily ...
Weijun Zhou's user avatar
  • 4,876
3 votes
0 answers
71 views

How come I can capture an object being constructed in the lambda doing the construction? [duplicate]

Consider the following code: int x = [&x](){ std::cout << "Inside lambda, x is " << x << '\n'; x = 5; return 23; }(); std::cout << "x is " &...
einpoklum's user avatar
  • 130k
7 votes
2 answers
574 views

Idiom for initializing an std::array using a generator function taking the index?

Suppose I have a function T foo(size_t i). What would be an elegant and succinct way of constructing an object arr, of type std::array<T, N>, so that we have arr[i] == foo(i)? If possible, I ...
einpoklum's user avatar
  • 130k
0 votes
1 answer
192 views

Snowflake query to create json - multiple results for single attribute

I have a table in snowflake that has three different columns being used as name type/values. These need to be mapped in an MDM tool using json. Of course, I cannot use the same attribute name more ...
Scott J's user avatar
  • 25
0 votes
1 answer
96 views

Constructing lambdas in constructors

I had some nasty bugs when constructing lambdas in constructors when working with callback-based event system. Here's simple scenario: class Object { public: int variable = 10; std::function&...
Rostys's user avatar
  • 79
0 votes
0 answers
33 views

Trouble constructing a map with immovable value types from initializer list [duplicate]

Consider the following code: #include <map> #include <type_traits> #include <memory> void foo() { using map_type = std::map<int, std::unique_ptr<float[]>>; auto ...
einpoklum's user avatar
  • 130k
1 vote
2 answers
230 views

Using a method to create objects

I have a question regarding creating object using a method, please see my code below: import java.util.ArrayList; import java.util.Scanner; public class Student { private String name; private ...
Jack M's user avatar
  • 71
0 votes
0 answers
41 views

Calling multiple async operations in a C# class initializer

Given a C# class like this public class MyData { public int Value1 { get; set; } public int Value2 { get; set; } } Could it ever create a concurrency problem to instantiate and initialize ...
R26's user avatar
  • 11
0 votes
1 answer
61 views

Mocking object construction in unit tests

I would like to write unit tests for a class which creates an instance of a problematic class, and I am working under the following constraints: My class calls some third-party library initialization ...
Danilo Piazzalunga's user avatar
5 votes
1 answer
770 views

Why does object.__new__ accept parameters?

Besides the obvious asking "again" about __new__ and __init__ in Python - I can ensure, I know what it does. I'll demonstrate some strange and to my opinion undocumented behavior, for which ...
Paebbels's user avatar
  • 16.1k
0 votes
1 answer
253 views

Python Factory Method Suggestions

I would like to ask you to help me out with my factory method. I have to handle multiple and periodic csv objects, each of which has its own properties, thus its own class. The only way I can tell ...
Marco's user avatar
  • 47
1 vote
1 answer
56 views

How can I create new objects with user defined properties every time a function is called in JavaScript?

Apologies if this is a stupid question, I'm very much a beginner here. I run a D&D game, and I thought a good thing to practice coding with would be a simple initiative tracker. At the moment, I'm ...
Holly Wilson's user avatar
1 vote
1 answer
448 views

Why does std::map crash when it is declared static inline inside a class and used early?

I have found several times that when a std::map is declared inside a class as a static inline (C++ 17), struct MyStruct { static inline std::map <A, B> mymap; MyStruct(A& a, B& ...
Peter Fletcher's user avatar
0 votes
3 answers
100 views

object Object being written [duplicate]

I am very new to JS and am playing around trying to output this verify function I came across below. In my limited thinking I thought that the result would be false and I would be able to write that ...
Mike Devine's user avatar
0 votes
2 answers
39 views

The value returned for a newly constructed object returns the value of the previously created one

I have the following challenge for practicing OOP in javascript: The Bank should allow us to: Add customers A customer should be able to deposit an amount. A customer should bevable to withdraw an ...
KN1368's user avatar
  • 103
0 votes
1 answer
102 views

Should a java constructor contain logic to build itself? [duplicate]

I'm working on a java project where I take in some JSON objects and have to then go through them to create a domain object that I need to then further process. Currently the constructor of this object ...
Astrum's user avatar
  • 611
3 votes
2 answers
98 views

F# Object Tree Syntax

In C# it is possible to construct object trees in a rather succint syntax: var button = new Button() { Content = "Foo" }; Is there an idiomatic way to do something similar in F#? Records ...
Bent Rasmussen's user avatar
0 votes
1 answer
151 views

How are objects from derived classes constructed in C++

Im not a C++ pro, i've done mostly Java and C#. A teacher said something that confused me today. I've tried to validate the info by doing some research, but i ended up even more confused. Lets say i ...
Frédéric Bélanger's user avatar
0 votes
0 answers
175 views

What is the difference between object creation and object initialization in C++?

This section of Richard Smith’s paper Guaranteed copy elision through simplified value categories makes an important distinction between object creation and object initialization (prvalues were ...
Géry Ogam's user avatar
  • 7,890
0 votes
1 answer
291 views

Can I call the garbage collector early in matlab?

In matlab, I have a class that ties up a shared resource during construction and releases it when deleted. (In my specific case, the shared resource is a port I can interact with it via http://...
bwall's user avatar
  • 1,050
2 votes
3 answers
193 views

Do we really need placement new-expressions?

I am trying to understand placement new-expressions in C++. This Stack Overflow answer states that T* p = new T(arg); is equivalent to void* place = operator new(sizeof(T)); // storage allocation T* ...
Géry Ogam's user avatar
  • 7,890
0 votes
0 answers
27 views

How do I correctly construct a new Python object? [duplicate]

Apologies for the slightly vague question, but I'm not too sure how to word it. The following very basic code example isn't doing what I'd expect it do, and I'd like to understand why... class Message(...
Grant's user avatar
  • 133
2 votes
1 answer
134 views

What's the difference between push_back({ "George", 1 }) and push_back(Student("Jack", 10));

I was just wondering if there's a difference between the following two lines: objStudents.push_back({ "George", 1 }); objStudents.push_back(Student("Jack", 10)); Is one way more ...
George Austin Bradley's user avatar
1 vote
0 answers
18 views

working of universal initialization causing destructor to be called one extra time [duplicate]

#include<iostream> #include<vector> class test { public: virtual ~test() { std::cout<<"...
shreyansh's user avatar
0 votes
0 answers
50 views

Javascript examples from Duckett's book not working

I am learning javascript from Duckett's book and have noticed that the examples from the book were not working when I copied them onto my text edit. The code is on the website so, I downloaded the ...
Sandy Enriquez's user avatar
1 vote
2 answers
102 views

constructing object from temporary

I'm using a third-party class with (only) constructor as follows class foo // cannot be altered { public: explicit foo(std::istream&); ... }; and the documentation of which suggests the ...
Walter's user avatar
  • 45.3k
3 votes
4 answers
5k views

how to maintain order of elements using snowflake object_construct() function instead of sorting by the keys?

Following snowflake query returns the JSON structure but output is sorted by the keys. How not to sort by the keys but retains the order? Is there any parameter setting that needs to be set? select ...
Samir's user avatar
  • 31
0 votes
1 answer
117 views

How to enforce that construction is always through a certain method?

I have some third-party abstract base class struct foo { virtual foo*job() = 0; ... static void* make_space(size_t sizeofDerived); }; which I cannot alter. Objects of type foo (and all ...
Walter's user avatar
  • 45.3k
3 votes
1 answer
2k views

Kotlin: How do you make a new instance of a data class with changes

I have a kotlin data class: data class MyCats ( ) { val name: String = "", val female: Boolean = false, val fixed: Boolean = false } As I understand Kotlin (still a newbie), I ...
SMBiggs's user avatar
  • 11.6k
11 votes
1 answer
694 views

What is the purpose for std::construct_at to cast through a pointer to volatile when using placement new?

According to cppreference, std::construct_at(T*p, Args&&... args) is equivalent to return ::new (const_cast<void*>(static_cast<const volatile void*>(p))) T(std::forward<Args&...
Walter's user avatar
  • 45.3k
-2 votes
1 answer
77 views

why do i not return anything from my constructor function?

function Person (name, eyeColor, age) { this.name = name; this.eyeColor = eyeColor; this.age = age; this.updateAge = function () { return ++this.age; }; } let person1 = new Person("kevin&...
user avatar
5 votes
3 answers
301 views

How to make an object constructor in JavaScript;

Could you please explain me what's wrong in the following code? function box (width, height, color) { this.width=width; this.height=height; this.color=color; this.style.width=width; ...
Alexandru Popescu's user avatar
1 vote
2 answers
56 views

how to get the member in C++ struct initialization

struct Test { int w, h; int * p; }; int main(){ Test t { 10, 20, new int[this->h*this->w] }; return 0; } I just want to use the w and h in ...
Biao Cao's user avatar
  • 151
-2 votes
1 answer
270 views

Problem constructing a base class from within a subclass constructor

I have 2 classes. Since Doctor will be considered as Employee, I should be using Employee class functions in Doctor class. Only extra thing that Doctor class has is TITLE. Basically, What I tried is I ...
Mert Özçelik's user avatar
0 votes
2 answers
216 views

Reconstructing JSON with jq

I have a JSON like this (sample.json): { "sheet1": [ { "hostname": "sv001", "role": "web", "ip1": "172.17.0.3" }, { "hostname": "sv002", "role": "web", ...
asby's user avatar
  • 13
0 votes
1 answer
1k views

Lombok: How to define a special method which runs upon the Object construction?

I have a @Value @AllArgsConstructor class, and I want each instance of it to have an ID(The first object has the ID of 1, the second is 2, etc..). until now, before I used lombok but a constructor, my ...
Techno Freak's user avatar
1 vote
1 answer
100 views

How do I include a previously declared function as a value in a key-value pair in an object constructor?

The task: declare a variable called myName that returns an array of two elements, firstname and last name. declare a function called join(), it will return the two elements as a string with a space ...
hotcoffeehero's user avatar
4 votes
3 answers
1k views

Java bad practice doing: new... ().doSomething()?

I just saw a piece of code that had some classes with only one method. I picked an examples: public class TempDirCleanupProcess { public void cleanup(final File directory) {} } Then, later on in ...
ItFreak's user avatar
  • 2,369
1 vote
1 answer
440 views

How usage of member initializer list prevents creation of redundant object in c++? [duplicate]

I have a question regarding difference in initializing an object with and without constructor member initializer list. In the following code snippet there are two classes Test1 and Test2 each with ...
ka_sh's user avatar
  • 607
0 votes
2 answers
622 views

Function Constructor - add function using prototype gives - Uncaught SyntaxError: Unexpected token {

I tried adding a function y() into the object constructor x using prototype chaining. It results to an unexpected error: Unexpected token { function x(a, b) { this.a = a this.b = b } ...
Monica Acha's user avatar
  • 1,086
1 vote
1 answer
229 views

Is it possible to limit the number of instances of a class at compile time?

Assume there is a class Foo that I, as the designer of a library, do not want my users to be able to instantiate more than n amount of times (where n is not necessarily 1). Is there any way to enforce ...
platisd's user avatar
  • 163
2 votes
3 answers
2k views

The Scope of Creating an instance of a class inside a method in java

Can anyone explain to me the scope of creating an object from a class inside a method in java, is it a bad way and wasting the resources or it's normal? The program is running but I am not sure about ...
KamelK's user avatar
  • 71
0 votes
1 answer
497 views

QML: object instantiation

Is there a way to have a mechanism similar to Component.createObject's second argument (initial properties) with a Loader element? I'm setting the properties manually in onLoaded, but this has ...
pmf's user avatar
  • 7,749
1 vote
0 answers
35 views

varadic argument templated object creation on container not working

Below is a example (not the entire class, just relevant parts) class Container { private: size_t m_next_id; std::unordered_map m_objects; public: template<typename Type, typename... ...
Glen Fletcher's user avatar
0 votes
1 answer
203 views

How do I name an Object inside of an Object Constructor someone's Discord Username?

I am trying to create an object constructor in a JSON file for Discord. The large constructor is named "person" and I want the objects inside of that to be the Discord Message Author's name, but I can'...
Logan Cook's user avatar
-3 votes
2 answers
77 views

Representing a value which can't be ctor-initialization-list initialized

I'm writing a class C with a member foo of type foo_t. This member must be defined and valid throughout the liftime of a C instance; however, I don't have the necessary information to construct it at ...
einpoklum's user avatar
  • 130k
14 votes
2 answers
10k views

std::initializer_list constructor

In code like this: #include <iostream> #include <initializer_list> #include <string> struct A { A() { std::cout << "2" << std::endl; } A(int a) { std::cout <&...
toozyfuzzy's user avatar
  • 1,196
-1 votes
2 answers
370 views

Delete object's variable after instantiation

So I have an object which needs certain variables to be instantiated. These variables are passed to the object through an array of objects. Then, each element in the array gets assigned to an internal ...
spcan's user avatar
  • 138