Click here to Skip to main content
15,886,362 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Title bar display and closing problem Pin
Richard MacCutchan6-May-23 0:03
mveRichard MacCutchan6-May-23 0:03 
GeneralRe: Title bar display and closing problem Pin
Jeanjean Lesage6-May-23 1:57
Jeanjean Lesage6-May-23 1:57 
QuestionCreating a pointer and asigning a variable address to it Pin
Calin Negru23-Apr-23 6:22
Calin Negru23-Apr-23 6:22 
AnswerRe: Creating a pointer and asigning a variable address to it Pin
Gerry Schmitz23-Apr-23 8:58
mveGerry Schmitz23-Apr-23 8:58 
AnswerRe: Creating a pointer and asigning a variable address to it Pin
Victor Nijegorodov23-Apr-23 20:14
Victor Nijegorodov23-Apr-23 20:14 
AnswerRe: Creating a pointer and asigning a variable address to it Pin
Richard MacCutchan23-Apr-23 21:26
mveRichard MacCutchan23-Apr-23 21:26 
GeneralRe: Creating a pointer and asigning a variable address to it Pin
Calin Negru23-Apr-23 22:39
Calin Negru23-Apr-23 22:39 
GeneralRe: Creating a pointer and asigning a variable address to it Pin
harold aptroot23-Apr-23 21:49
harold aptroot23-Apr-23 21:49 
The purpose of delete is not to delete things. What?? But I'm serious. The purpose of delete is to undo new.

As an approximation that will no doubt anger several C++ experts, you can view new as a combination of malloc and invoking the constructor, and you can view delete as a combination of invoking the destructor and free. That's not exactly literally how they work, but it's close enough that you can use that to reason about when you need them. (don't use this as an excuse to delete something that was malloc'ed)

Most of the time, if you want to get rid of something, delete is the wrong way to do it - because most things were not created by new. Trying to delete something else results at best in a failing safety check but in general corrupts the state of your application.

Most objects are destroyed implicitly, in various ways. Local variables by exiting a scope, values in containers by the container being destroyed, smart pointers do most of the rest. Explicitly using delete is occasionally necessary but it's probably better to actively avoid creating those cases. It's quite bug-prone, especially around exceptions and other non-trivial control flow.
Calin Negru wrote:
later if you use “delete” on the pointer will that delete the content of the variable too?
In the sense that its destructor is invoked (so you can implement it and delete the members that need to be deleted), yes. If you mean that in some other sense then, probably, no (but depending on what you mean).

Implementing classes with lots of raw-pointer members that you individually delete in the destructor is very mid-2000s. Typical advice is to delegate that to smart pointers and containers as much as possible, with the goal of following the Rule of Zero instead of one of the other rules-of-some-number, but sometimes you just have to do what you have to do.
GeneralRe: Creating a pointer and asigning a variable address to it Pin
CPallini25-Apr-23 20:27
mveCPallini25-Apr-23 20:27 
QuestionMessage Closed Pin
22-Apr-23 6:51
Member 1496877122-Apr-23 6:51 
AnswerRe: How to implement "\n" in QT QDebug class Pin
k505422-Apr-23 7:32
mvek505422-Apr-23 7:32 
GeneralMessage Closed Pin
22-Apr-23 13:00
Member 1496877122-Apr-23 13:00 
GeneralRe: How to implement "\n" in QT QDebug class Pin
Richard MacCutchan22-Apr-23 21:10
mveRichard MacCutchan22-Apr-23 21:10 
Questionget text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Ilqar Sadiqov8-Apr-23 9:02
Ilqar Sadiqov8-Apr-23 9:02 
AnswerRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Richard MacCutchan8-Apr-23 21:01
mveRichard MacCutchan8-Apr-23 21:01 
GeneralRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Ilqar Sadiqov9-Apr-23 20:46
Ilqar Sadiqov9-Apr-23 20:46 
GeneralRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Richard MacCutchan9-Apr-23 22:14
mveRichard MacCutchan9-Apr-23 22:14 
GeneralRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Dave Kreskowiak10-Apr-23 1:59
mveDave Kreskowiak10-Apr-23 1:59 
Questionc++ Pin
EvelynChristian7-Apr-23 5:01
EvelynChristian7-Apr-23 5:01 
AnswerRe: c++ Pin
Victor Nijegorodov7-Apr-23 6:45
Victor Nijegorodov7-Apr-23 6:45 
GeneralRe: c++ Pin
Richard MacCutchan7-Apr-23 7:20
mveRichard MacCutchan7-Apr-23 7:20 
GeneralRe: c++ Pin
Victor Nijegorodov7-Apr-23 7:30
Victor Nijegorodov7-Apr-23 7:30 
GeneralRe: c++ Pin
jschell7-Apr-23 9:13
jschell7-Apr-23 9:13 
GeneralRe: c++ Pin
Richard MacCutchan7-Apr-23 21:57
mveRichard MacCutchan7-Apr-23 21:57 
QuestionSnooker Game programing using c Programing. Pin
Francis Lagar3-Apr-23 7:37
Francis Lagar3-Apr-23 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.