Questions tagged [object-construction]

For questions related to object construction, usually in an OOP environment.

Construction is a concept of Object-Oriented languages.

Objects need to be setup for use:

  • Their member variables need to be allocated
  • Any Object member variables also need to be constructed
  • The Object methods need to be setup for calling

This process is accomplished by construction.

95 questions
2
votes
3 answers

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* p = new(place) T(arg); // object…
2
votes
1 answer

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 efficient than the other? Or is it just a different syntax that…
2
votes
3 answers

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 object_construct ( 'entity', 'XYZ', 'allowed',…
2
votes
3 answers

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 this step: /** * Sending Packets Method *…
2
votes
1 answer

Object creation and destruction order in C++

I wrote a simple program to learn more about the order of creating and destructing objects in C++ (using Visual Studio 2015). Here it is: #include #include using namespace std; class A { public: A(string name) :…
NPS
  • 6,003
  • 11
  • 53
  • 90
2
votes
2 answers

Using placement new in a container

I just came across some container implementation in C++. That class uses an internal buffer to manage its objects. This is a simplified version without safety checks: template class Container { public: Container() : buffer(new…
2
votes
4 answers

Creating objects from static properties in python

I have a Category class which has different names for each categories, the names of the categories can be unknown, good and bad, all categories share the same behavior so i don't want to create sub classes for each type of category, the problem…
angvillar
  • 1,074
  • 3
  • 10
  • 25
1
vote
1 answer

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 just trying to create a new object with user-defined…
1
vote
1 answer

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 mymap; MyStruct(A& a, B& b) { mymap[a] = b; } }; the MyStruct…
1
vote
0 answers

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

#include #include class test { public: virtual ~test() { std::cout<<"inside destructor \n"; } }; …
shreyansh
  • 11
  • 2
1
vote
2 answers

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 following approach std::ifstream…
Walter
  • 44,150
  • 20
  • 113
  • 196
1
vote
1 answer

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 can instantiate this class and set all its parameters at…
SMBiggs
  • 11,034
  • 6
  • 68
  • 83
1
vote
2 answers

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 initialization, is there any way to get this?
Biao Cao
  • 141
  • 1
  • 10
1
vote
1 answer

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 between each element. declare a function…
1
vote
1 answer

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

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 two constructors, objects of these two classes are…
sh_ark
  • 557
  • 5
  • 14