object-construction

Using placement new in a container

旧街凉风 提交于 2021-02-11 05:03:38
问题 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 <typename E> class Container { public: Container() : buffer(new E[100]), size(0) {} ~Container() { delete [] buffer; } void Add() { buffer[size] = E(); size++; } void Remove() { size--; buffer[size].~E(); } private: E* buffer; int size; }; AFAIK this will construct/destruct E objects redundantly in Container() and

Object creation and destruction order in C++

扶醉桌前 提交于 2020-01-16 03:50:40
问题 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 <iostream> #include <string> using namespace std; class A { public: A(string name) : name(name) { cout << "A(" << name << ")::constructor()" << endl; } ~A() { cout << "A(" << name << ")::destructor()" << endl; } private: string name; }; class C { public: C(string name, A a) : name(name), a(a) { cout << "C(" << name << ")::constructor()" << endl; }

Object creation and destruction order in C++

只愿长相守 提交于 2020-01-16 03:50:30
问题 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 <iostream> #include <string> using namespace std; class A { public: A(string name) : name(name) { cout << "A(" << name << ")::constructor()" << endl; } ~A() { cout << "A(" << name << ")::destructor()" << endl; } private: string name; }; class C { public: C(string name, A a) : name(name), a(a) { cout << "C(" << name << ")::constructor()" << endl; }

How do I initialize classes with lots of fields in an elegant way?

二次信任 提交于 2019-12-29 11:35:29
问题 In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like this: public void testRequest() { //All these below used classes are generated classes from xsd schema file. CheckRequest checkRequest = new CheckRequest(); Offers offers = new Offers(); Offer offer = new Offer(); HotelOnly hotelOnly = new HotelOnly(); Hotel

How do I initialize classes with lots of fields in an elegant way?

六眼飞鱼酱① 提交于 2019-12-29 11:35:23
问题 In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like this: public void testRequest() { //All these below used classes are generated classes from xsd schema file. CheckRequest checkRequest = new CheckRequest(); Offers offers = new Offers(); Offer offer = new Offer(); HotelOnly hotelOnly = new HotelOnly(); Hotel

How can I initialize C++ object member variables in the constructor?

大憨熊 提交于 2019-12-27 16:49:47
问题 I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O On StackOverflow, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this: class MyClass { public: MyClass(int n); private: AnotherClass another(100); // this constructs AnotherClass

Extending prototype on object literal

扶醉桌前 提交于 2019-12-25 03:43:07
问题 If I have the following code, why does it return an error saying Cannot set property 'second_prop' of undefined . I thought that you can extend the prototype property and add more variables and methods to the object prototype. Since the two console statements return 'Object' and true, then why does it return an error of undefined. My thinking is, if the 'obj' is an object of type Object, then I should be able to do temp.prototype.newproperty? So then the Object will have a 'newproperty'. But

Extending prototype on object literal

大憨熊 提交于 2019-12-25 03:42:09
问题 If I have the following code, why does it return an error saying Cannot set property 'second_prop' of undefined . I thought that you can extend the prototype property and add more variables and methods to the object prototype. Since the two console statements return 'Object' and true, then why does it return an error of undefined. My thinking is, if the 'obj' is an object of type Object, then I should be able to do temp.prototype.newproperty? So then the Object will have a 'newproperty'. But

Need a design pattern to remove enums and switch statement in object creation

只谈情不闲聊 提交于 2019-12-11 07:19:44
问题 Let's say I am creating a sports game, and in this game there are various positions a player can play, attack, defence, etc. So I start by creating a base class: public abstract class Position { public abstract string Name { get; } } and the subclasses... public class Defender : Position { public override string Name { get { return "Defender"; } } } and so on. This is all fine. But now I need a function to create these objects. I need a create by position function. So one possible solution is

Does Java have a default copy constructor (like in C++)? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-08 17:01:52
问题 This question already has answers here : Why doesn't Java have a copy constructor? (9 answers) Closed 3 years ago . Does Java has a default copy constructor as C++? If it has one - does it remain usable if I declare another constructor (not a copy constructor) explicitly? 回答1: Java does not have bulit-in copy constructors. But you can write your own such constructors. See an example below: class C{ private String field; private int anotherField; private D d; public C(){} public C(C other){