new-operator

Instantiate an array of objects, in simpliest way?

强颜欢笑 提交于 2019-12-06 04:22:09
Given a class: class clsPerson { public int x, y; } Is there some way to create an array of these classes with each element initialized to a (default) constructed instance, without doing it manually in a for loop like: clsPerson[] objArr = new clsPerson[1000]; for (int i = 0; i < 1000; ++i) objArr[i] = new clsPerson(); Can I shorten the declaration and instantiation of an array of N objects? You must invoke the constructor for each item. There is no way to allocate an array and invoke your class constructors on the items without constructing each item. You could shorten it (a tiny bit) from a

Dynamic allocation with DOUBLE POINTERS

。_饼干妹妹 提交于 2019-12-06 04:08:55
问题 I have a base class Toy and derived classes Toy_remote_car amd Toy_battery_car. I am doing this: Toy** ptr; ptr=new Toy*; ptr[0]=new Toy_remote_car[1]; ptr[1]=new Toy_battery_car[1];/*this is completely wrong according to my teacher because i never created ptr[1]. Instead this is a misuse of memory according to him.*/ The above code(ptr=new Toy*) is creating a single pointer of type Toy(ptr[0]) which contains the object of derived class Toy_remote_car. Now i want to write such a code: ->the

To “new” or not to “new”

隐身守侯 提交于 2019-12-06 01:59:14
问题 Is there a rule of thumb to follow when to use the new keyword and when not to when declaring objects? List<MyCustomClass> listCustClass = GetList(); OR List<MyCustomClass> listCustClass = new List<MyCustomClass>(); listCustClass = GetList(); 回答1: In your scenario it seems that the actual creation of the object is being performed inside your GetList() method. So your first sample would be the correct usage. When created, your List<MyCustomClass> is stored in the heap, and your listCustClass

Reallocating memory of a C++ array. [duplicate]

六眼飞鱼酱① 提交于 2019-12-06 01:39:35
This question already has answers here : Closed 7 years ago . Possible Duplicate: How do you realloc in C++? I know that C++ arrays can be reallocated (expanded) using realloc() if memory has been allocated via malloc() or calloc() . My question is, how can I expand an array in C++ whose memory has been allocated via the new operator? You can't - that's why in C++ you use std::vector<> . If you wanted to do this, you'd have to allocate a new array (via new ), then copy the old items across ( std::copy for example), then delete[] the previous array. Just use std::vector - let it do all that

Javascript `new` keyword on function returning array

Deadly 提交于 2019-12-06 01:37:31
问题 I was experimenting with the new keyword and I can't find an explanation for this behavior. Let's say we have a function returning an integer: (In firebug) >>> function x() { return 2; } >>> x() 2 >>> new x() x { } But if the function returns an array : >>> function y() { return [2]; } >>> y() [2] >>> new y() [2] Why is that ? 回答1: The new operator has an interesting behavior: It returns the object created by the operator unless the constructor function returns a different object. Any non

Can someone explain exactly what happens if an exception is thrown during the process of allocating an array of objects on the heap?

荒凉一梦 提交于 2019-12-06 01:23:48
I defined a class foo as follows: class foo { private: static int objcnt; public: foo() { if(objcnt==8) throw outOfMemory("No more space!"); else objcnt++; } class outOfMemory { public: outOfMemory(char* msg) { cout << msg << endl;} }; ~foo() { cout << "Deleting foo." << endl; objcnt--;} }; int foo::objcnt = 0; And here's the main function: int main() { try { foo* p = new foo[3]; cout << "p in try " << p << endl; foo* q = new foo[7]; }catch(foo::outOfMemory& o) { cout << "Out-of-memory Exception Caught." << endl; } } It is obvious that the line "foo* q = new foo[7];" only creates 5 objects

Is such assignment a good idea in C++

Deadly 提交于 2019-12-06 01:21:41
A lot of classes has assignment operator (operator=) the same code as in destructor and than very similar code of copy constructor. So is it good idea to implement the assignment in such way? Point& operator=(const Point& point) { if(&point != this) { //Call the destructor this->~Point(); //Make the placement new //Assignment is made because some compilers optimise such code as just // new Point; Point* p_n = new (this) Point(point); //We where placing in this place so pointers should be equal assert(p_n == this); } return *this; } Fred Larson Herb Sutter has addressed this in one of his GotW

Is it ok to return None from __new__?

空扰寡人 提交于 2019-12-06 00:42:51
问题 In general, is it reasonable to return None from a __new__ method if the user of the class knows that sometimes the constructor will evaluate to None? The documentation doesn't imply it's illegal, and I don't see any immediate problems (since __init__ is not going to be called, None not being an instance of the custom class in question!). But I'm worried about whether it might have other unforeseen issues whether it's a good programming practice to have constructors return None Specific

Why would you want to hide a method using `new`? [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-05 22:59:41
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C# - new keyword in method signature Let's say I have 3 classes: GrandDad, Dad, Son. Son inherits from Dad, which inherits from GrandDad. Each class implements foo. // GrandDad class: public virtual void foo() // Dad class: new public virtual void foo() // Son class: public override void foo() I don't understand the reason as to why Dad would use the new keyword. As I understand, using new hides a method. Why

What is the meaning of the below sentence in c++ [duplicate]

[亡魂溺海] 提交于 2019-12-05 22:26:04
Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents, is it the way of typecasting? or what void someCode() { char memory[sizeof(Fred)]; // Line #1 void* place = memory; // Line #2 Fred* f = new(place) Fred(); // Line #3 // The pointers f and place will be equal ... } This is a typical usage of Placement new . It allows you to allocate memory and then construct objects at that particular memory location. Line #3 essentially just calls the constructor Fred::Fred() . The this pointer in the Fred constructor will be equal to place . The returned pointer f will