new-operator

More about Virtual / new…plus interfaces!

☆樱花仙子☆ 提交于 2019-12-18 13:36:33
问题 Yesterday I posted a question about the new/virtual/override keywords, and i learned a lot from your answers. But still i remain with some doubts. In between all the "boxes", i lost touch with what's really happening to the type's method tables. For instance: interface I1 { void Draw(); } interface I2 { void Draw(); } class A : I1, I2 { public void Minstance() { Console.WriteLine("A::MInstance"); } public virtual void Draw() { Console.WriteLine("A::Draw"); } void I2.Draw() { Console.WriteLine

Dynamically creating an instance of a class from a string containing the class name in C++

拈花ヽ惹草 提交于 2019-12-18 13:07:30
问题 Lets say I have a base class with 100 children: class Base { virtual void feed(); ... }; class Child1 : public Base { void feed(); //specific procedure for feeding Child1 ... }; ... class Child100 : public Base { void feed(); //specific procedure for feeding Child100 ... }; At runtime I want to read a file that contains which children to create and feed. Lets say I've read the file and the vector of strings "names" contains the names of the child classes (ie. Child1, Child4, Child99). Now I'm

Does ::operator new(size_t) use malloc()?

好久不见. 提交于 2019-12-18 13:01:44
问题 Does ::operator new(size_t) call malloc() internally, or does it use system calls / OS-specific library calls directly? What does the C++ standard say? In this answer it says that: malloc() is guaranteed to return an address aligned for any standard type. ::operator new(n) is only guaranteed to return an address aligned for any standard type no larger than n , and if T isn't a character type then new T[n] is only required to return an address aligned for T . And that suggests that new()

Confused by behavior of `map` on arrays created using `new` [duplicate]

社会主义新天地 提交于 2019-12-18 12:15:58
问题 This question already has answers here : Undefined values in Array(len) initializer (5 answers) Closed 4 years ago . I am confused by the results of map ping over an array created with new : function returnsFourteen() { return 14; } var a = new Array(4); > [undefined x 4] in Chrome, [, , , ,] in Firefox a.map(returnsFourteen); > [undefined x 4] in Chrome, [, , , ,] in Firefox var b = [undefined, undefined, undefined, undefined]; > [undefined, undefined, undefined, undefined] b.map

Overallocating with new/delete

邮差的信 提交于 2019-12-18 02:49:35
问题 Using malloc and free , it is easy to allocate structures with extra data beyond the end. But how do I accomplish the same with new / delete ? I know I could use placement new syntax along with malloc for the allocation part, but will delete work properly and portably if I place an object in memory allocated by malloc ? What I want to accomplish is the same as the following example, but using new / delete instead of malloc / free , so that constructors/destructors will be called properly:

Policy with catching std::bad_alloc

◇◆丶佛笑我妖孽 提交于 2019-12-18 02:40:15
问题 So I use Qt a lot with my development and love it. The usual design pattern with Qt objects is to allocate them using new . Pretty much all of the examples (especially code generated by the Qt designer) do absolutely no checking for the std::bad_alloc exception. Since the objects allocated (usually widgets and such) are small this is hardly ever a problem. After all, if you fail to allocate something like 20 bytes, odds are there's not much you can do to remedy the problem. Currently, I've

operator new overloading and alignment

我与影子孤独终老i 提交于 2019-12-17 22:30:01
问题 I'm overloading operator new , but I recently hit a problem with alignment. Basically, I have a class IBase which provides operator new and delete in all required variants. All classes derive from IBase and hence also use the custom allocators. The problem I'm facing now is that I have a child Foo which has to be 16-byte aligned, while all others are fine when aligned to 8-byte. My memory allocator however aligns to 8-byte boundaries only by default, so now the code in IBase::operator new

C++ Object without new

…衆ロ難τιáo~ 提交于 2019-12-17 21:26:16
问题 this is a really simple question but I havn't done c++ properly for years and so I'm a little baffled by this. Also, it's not the easiest thing (for me at least) to look up on the internet, not for trying. Why doesn't this use the new keyword and how does it work? Basically, what's going on here? CPlayer newPlayer = CPlayer(position, attacker); 回答1: This expression: CPlayer(position, attacker) creates a temporary object of type CPlayer using the above constructor, then: CPlayer newPlayer =...

Can I use apply() with constructor to pass arbitrary number of parameters

寵の児 提交于 2019-12-17 19:44:40
问题 I've got a function wich can accept a varible number of parameter with a rest operator. I want create an object passing the argument collected with the rest operator directly to a constructor without create an object and call an initializing function and without passing the entire array but the parameters ah I do with apply() function. Is it possible ? Using apply doesn't work. public function myFunc(...arg) { // something link "new MyClass.apply(args)" return new MyClass(); } 回答1:

Add new data into PHP JSON string

谁说我不能喝 提交于 2019-12-17 19:38:44
问题 I have $data as JSON encoded data and I have this string: $new_data = "color:'red'"; that needs to be added to $data so that I can read it from it as a json string. How can I achieve this ? 回答1: you need to json_decode($data) first, then add the new key/value, and json_encode() it. 回答2: I was just searching for the solution to this and stumbled across this question (already one year old). The answers provided so far were not very helpful to me. So, hopefully this helps the next person. The