new-operator

Python: How can I inherit from the built-in list type?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 18:28:10
问题 I want to add some attributes to the built-in list type, so I wrote this: class MyList(list): def __new__(cls, *args, **kwargs): obj = super(MyList, cls).__new__(cls, *args, **kwargs) obj.append('FirstMen') return obj def __init__(self, *args, **kwargs): self.name = 'Westeros' def king(self): print 'IronThrone' if __name__ == '__main__': my_list = MyList([1, 2, 3, 4]) print my_list but my_list contains only the element 'FirstMen' . Why my __new__ doesn't work here? And how should I inherit

Overloading operator new for a class

一笑奈何 提交于 2019-12-17 18:08:54
问题 When we overload new operator of a class, we declare the function as a member function. For eg: class OpNew { public: OpNew() { cout << "OpNew::OpNew()" << endl;} void* operator new(size_t sz) { cout << "OpNew::new: " << sz << " bytes" << endl; return ::new char[sz]; } }; How does the statement OpNew *obj = new OpNew work under the hood ? as overloaded new is a member of OpNew class not a static. So how does compiler ensure this call to new member function succeeds? 回答1: An operator new() or

Why new keyword not needed for String

旧街凉风 提交于 2019-12-17 17:34:13
问题 I am new in java. In java, String is a class .But we do not have to use new keyword to create an object of class String where as new is used for creating objects for other classes. I have heard about Wrapper classes like Integer , Double which are similar to this. But String is not Wrapper,isn't it? Actually what is happening when i use String message = "Hai"; ?? How it is different from String message = new String("Hai"); Here is message a reference variable or something else?? Are there

How to cleanly initialize attributes in Ruby with new?

☆樱花仙子☆ 提交于 2019-12-17 16:27:32
问题 class Foo attr_accessor :name, :age, :email, :gender, :height def initalize params @name = params[:name] @age = params[:age] @email = params[:email] . . . end This seems like a silly way of doing it. What is a better/more idiomatic way of initalizing objects in Ruby? Ruby 1.9.3 回答1: def initialize(params) params.each do |key, value| instance_variable_set("@#{key}", value) end end 回答2: You can just iterate over the keys and invoke the setters. I prefer this, because it will catch if you pass

What are uses of the C++ construct “placement new”?

ぐ巨炮叔叔 提交于 2019-12-17 15:46:23
问题 I just learned about the C++ construct called "placement new". It allows you to exactly control where a pointer points to in memory. It looks like this: #include <new> // Must #include this to use "placement new" #include "Fred.h" // Declaration of class Fred void someCode() { char memory[sizeof(Fred)]; void* place = memory; Fred* f = new(place) Fred(); // Create a pointer to a Fred(), // stored at "place" // The pointers f and place will be equal ... } (example from C++ FAQ Lite) In this

Can multithreading speed up memory allocation?

笑着哭i 提交于 2019-12-17 15:39:55
问题 I'm working with an 8 core processor, and am using Boost threads to run a large program. Logically, the program can be split into groups, where each group is run by a thread. Inside each group, some classes invoke the 'new' operator a total of 10000 times. Rational Quantify shows that the 'new' memory allocation is taking up the maximum processing time when the program runs, and is slowing down the entire program. One way I can speed up the system could be to use threads inside each 'group',

How to keep my user input on the same line after an output?

♀尐吖头ヾ 提交于 2019-12-17 14:56:33
问题 I'm trying to write code that asks for the users age and then they enter it, but I want the number to appear next to the question after you enter it. My code looks like this: System.out.println("Enter a number: "); num1 = userIn.nextInt(); It works fine, but the number always appears on the line below. Output: Enter a number: 12 What I want: Enter a number: 12 Any suggestions? 回答1: Use print() instead of println() ; System.out.println() automatically adds a newline character. That what the ln

How does require work with new operator in node.js?

梦想的初衷 提交于 2019-12-17 12:25:16
问题 Let's have a file.js with this code: module.exports.func = function(txt) { this.a = 1; this.b = 2; console.log(txt, this); return this; } Now we have another JS file where we do following: var r1 = new (require('./file')).func('r1'); var r2 = new require('./file').func('r2'); In r1 case it works as intended - r1 contains reference to the newly created object. In r2 case it does not work - r2 gets reference to module.exports from within the file.js. The intention was to create a new object by

What does the `new` keyword do

人盡茶涼 提交于 2019-12-17 09:34:06
问题 I'm following a Java tutorial online, trying to learn the language, and it's bouncing between two semantics for using arrays. long results[] = new long[3]; results[0] = 1; results[1] = 2; results[2] = 3; and: long results[] = {1, 2, 3}; The tutorial never really mentioned why it switched back and forth between the two so I searched a little on the topic. My current understanding is that the new operator is creating an object of "array of longs" type. What I do not understand is why do I want

Does new[] call default constructor in C++?

倖福魔咒の 提交于 2019-12-17 07:15:36
问题 When I use new[] to create an array of my classes: int count = 10; A *arr = new A[count]; I see that it calls a default constructor of A count times. As a result arr has count initialized objects of type A . But if I use the same thing to construct an int array: int *arr2 = new int[count]; it is not initialized. All values are something like -842150451 though default constructor of int assignes its value to 0 . Why is there so different behavior? Does a default constructor not called only for