new-operator

When to use new instead of override C# [duplicate]

强颜欢笑 提交于 2019-12-10 01:11:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C# keyword usage virtual+override vs. new Difference between new and override? So I've been working on a project and decided to do some reading about the difference between the new and override keywords in C#. From what I saw, it seems that using the new keyword functionality is a great way to create bugs in the code. Apart from that I don't really see when it would actually make sense to use it. More out of

overloading new and delete

寵の児 提交于 2019-12-09 21:25:06
问题 I try to follow this article: http://flipcode.com/archives/How_To_Find_Memory_Leaks.shtml to overload my new and delete functions in order to track memory leaks. however - if I try to compile, I get a C2365: "operator new": redefinition; previous definition was a "function" in the file xdebug xdebug gets included in xlocale - however, i can't find where my project is including xlocale I'm using MFC for multithreading in my project. Can someone tell me how I can get my memory leak tracking to

new operator in function call

て烟熏妆下的殇ゞ 提交于 2019-12-09 20:06:18
问题 My question is what happens to the object allocated with the new operator that is inside a function call. A specific example: I have a private vector pV which I want to send to a object/function outside of the class, foo->func(std::vector<int> *vec) . I first tried to write foo->func( new std::vector<int>(pV) ) but this resulted in a memory leak (when said function is called repeatedly inside a loop). When I specifically created a new object, called the function and then deleted that object,

VIM: set filetype=txt for every new file [No Name]

倖福魔咒の 提交于 2019-12-09 17:21:28
问题 I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work. This is p.e. what I've read on the web a few times: au BufRead,BufNewFile *.txt setlocal ft=txt (to put in _vimrc) However it doesn't work. Can anyone help me? 回答1: The following line, added to your .vimrc , will set the filetype to text if it is not already set. autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif 回答2: All files are considered plain text

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

戏子无情 提交于 2019-12-09 12:06:44
问题 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

Use new operator to initialise an array

穿精又带淫゛_ 提交于 2019-12-09 04:37:29
问题 I want to initialise an array in the format that uses commas to separate the elements surrounded in curly braces e.g: int array[10]={1,2,3,4,5,6,7,8,9,10}; However, I need to use the new operator to allocate the memory e.g: int *array = new int[10]; Is there a way to combine theses methods so that I can allocate the memory using the new operator and initialise the array with the curly braces ? 回答1: You can use memcpy after the allocation. int originalArray[] ={1,2,3,4,5,6,7,8,9,10}; int

What is the purpose of hiding (using the “new” modifier) an interface method declaration?

僤鯓⒐⒋嵵緔 提交于 2019-12-08 14:58:25
问题 it's possible to mark a method declaration in an interface as " new " but does it have any "technical" sense or is it just a way to explicitly state that the declaration cannot override a previous one ? For example : interface II1 { new void F(); } interface II2 : II1 { new void F(); } is valid (the C# 4.0 compiler does not complain) but does not appear to be different from : interface II1 { void F(); } interface II2 : II1 { void F(); } Thanks in advance for any information. EDIT: do you know

Creation of an object in C++

一曲冷凌霜 提交于 2019-12-08 14:09:30
问题 Isn't A a = new A(); // A is a class name supposed to work in C++? I am getting: conversion from 'A*' to non-scalar type 'A' requested Whats wrong with that line of code? This works in Java, right? Also, what is the correct way to create a new object of type A in C++, then? 回答1: No, it isn't. The new operation returns a pointer to the newly created object, so you need: A * a = new A(); You will also need to manage the deletion of the object somewhere else in your code: delete a; However,

Problem allocating derived class array with new

烂漫一生 提交于 2019-12-08 09:19:16
问题 I have a simple program $ cat a.cpp #include <iostream> class MyClass { public: virtual void check() { std::cout << "Inside MyClass\n"; } }; class MyClass2: public MyClass { public: int* a; virtual void check() { std::cout << "Inside MyClass2\n"; } }; int main() { MyClass *w, *v; w = new MyClass2[2]; v = new MyClass2; std::cout << "Calling w[0].check\n"; w[0].check(); std::cout << "Calling v->check\n"; v->check(); std::cout << "Calling w[1].check\n"; w[1].check(); } $ g++ a.cpp $ ./a.out

Regarding placement new in C++

孤人 提交于 2019-12-08 08:24:23
问题 I am having a problem with the placement new operator. I have two programs: Program1 (operator.cpp) and Program2 (main.cpp): Program1: operator.cpp void *operator new(size_t size) { void *p; cout << "From normal new" << endl; p=malloc(size); return p; } void *operator new(size_t size, int *p) throw() { cout << "From placement new" << endl; return p; } Here is the second program to which the first is linked: main.cpp: #include <new> int main() { int *ptr=new int; int *ptr1=new(ptr) int(10); }