new-operator

Will new operator return NULL? [duplicate]

空扰寡人 提交于 2019-12-03 20:37:16
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Will new return NULL in any case? Say i have a class Car and i create an object Car *newcar = new Car(); if(newcar==NULL) //is it valid to check for NULL if new runs out of memory { } 回答1: On a standards-conforming C++ implementation, no. The ordinary form of new will never return NULL ; if allocation fails, a std::bad_alloc exception will be thrown (the new (nothrow) form does not throw exceptions, and will

What side effects does the keyword 'new' have in JavaScript?

六眼飞鱼酱① 提交于 2019-12-03 15:04:57
问题 I'm working on a plug-in for jQuery and I'm getting this JSLint error: Problem at line 80 character 45: Do not use 'new' for side effects. (new jQuery.fasterTrim(this, options)); I haven't had much luck finding info on this JSLint error or on any side effects that new might have. I've tried Googling for "Do not use 'new' for side effects." and got 0 results. Binging gives me 2 results but they both just reference the JSLint source. Hopefully this question will change that. :-) Update #1: Here

Can you use the C# new keyword to expand properties on an interface?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 14:31:21
I understand how the "new" keyword can hide methods in a derived class. However, what implications does it have for classes that implement interfaces that use the keyword? Consider this example, where I decide to expand an interface by making its properties read/write. public interface IReadOnly { string Id { get; } } public interface ICanReadAndWrite : IReadOnly { new string Id { get; set; } } Then you are able to do things like this: public IReadOnly SomeMethod() { // return an instance of ICanReadAndWrite } Is this bad design? Will it cause issues for my classes that implement

How is the C++ 'new' operator implemented

风格不统一 提交于 2019-12-03 13:32:10
问题 Class B; B *b = new B(); // default constructor B *b1 = new B(10); // constructor which takes an argument B(int x) However, if we want to write a custom version of new , the syntax is Class B { /*...*/ static void* operator new(size_t size); } How is the statement new B() converted to a function call for operator new(sizeof(B)) ? And how does it keep track of which constructor to call i.e. how does it distinguish between new B() and new B(int x) ? Is new implemented as a macro in C++? 回答1:

Can C++0x still explicitly allocate with global operator new?

喜你入骨 提交于 2019-12-03 11:22:21
问题 Wikipedia states: A type can be made impossible to allocate with operator new: struct NonNewable { void *operator new(std::size_t) = delete; }; An object of this type can only ever be allocated as a stack object or as a member of another type. It cannot be directly heap-allocated without non-portable trickery. (Since placement new is the only way to call a constructor on user-allocated memory and this use has been forbidden as above, the object cannot be properly constructed.) Deleting

Deleting an object in C++

爱⌒轻易说出口 提交于 2019-12-03 07:34:33
问题 Here is a sample code that I have: void test() { Object1 *obj = new Object1(); . . . delete obj; } I run it in Visual Studio, and it crashes at the line with 'delete obj;'. Isn't this the normal way to free the memory associated with an object? I realized that it automatically invokes the destructor... is this normal? Here is a code snippet: if(node->isleaf()) { vector<string> vec = node->L; vec.push_back(node->code); sort(vec.begin(), vec.end()); Mesh* msh = loadLeaves(vec, node->code);

What does 'new' keyword mean when used inside an interface in C#?

烈酒焚心 提交于 2019-12-03 06:29:07
问题 Developing an interface generic I wished to declare a constructor in an interface but it says constructors are forbidden there. I've tried to declare a static factory method then, but it says neither static methods are allowed and suggests using 'new' keyword. But I have hardly any idea of what could 'new' keyword exactly mean when used inside an interface in C#. Have you? UPDATE: I didn't post any sample code because I didn't want to mix 2 questions - how to specify a constructor/factory in

When is the appropriate time to use the 'new' keyword?

瘦欲@ 提交于 2019-12-03 06:25:12
When is it necessary to use the new keyword in Java. I know you are supposed to use it when you create an instance of an object like this: TextView textView = new TextView(this); Sometimes in code I notice that new isn't used and I get confused.. In this line of code: AssetManager assetManager = getAssets(); Why isn't an instance of the AssetManager created like this: AssetManager assetManager = new AssetManager(); then it is set equal to getAssests()? When should new be used? Thanks! You use the new keyword when an object is being explicitly created for the first time. Then fetching an object

creating an array of object pointers C++

给你一囗甜甜゛ 提交于 2019-12-03 06:01:09
问题 I want to create an array that holds pointers to many object, but I don't know in advance the number of objects I'll hold, which means that I need to dynamically allocate memory for the array. I have thought of the next code: ants = new *Ant[num_ants]; for (i=1;i<num_ants+1;i++) { ants[i-1] = new Ant(); } where ants is defined as Ant **ants; and Ant is a class. Will it work? 回答1: Will it work? Yes. However, if possible, you should use a vector: #include <vector> std::vector<Ant*> ants; for

C++: is push_back(new Object()) a memory leak?

狂风中的少年 提交于 2019-12-03 05:27:13
Is the following C++ code a memory leak? list.push_back(new String("hi")); As I understand it, push_back from any std collection/container always makes a copy. So if the new string is copied, nothing can ever delete the new'd string right? since there is no reference to it after the push_back... Am I correct or wrong here? Thanks. Jbu edit: I think I am wrong, since new will return a pointer...we'll always have the pointer to be able to delete the new String No, the vector stores pointers and the copy is made of the pointer. You can delete the object any time later. (You may get a leak, if the