Click here to Skip to main content
15,880,427 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: ListView / Win32 Pin
Fareed Rizkalla11-Apr-10 23:29
Fareed Rizkalla11-Apr-10 23:29 
GeneralRe: ListView / Win32 Pin
Richard MacCutchan18-Apr-10 4:45
mveRichard MacCutchan18-Apr-10 4:45 
AnswerRe: ListView / Win32 Pin
peterchen12-Apr-10 20:28
peterchen12-Apr-10 20:28 
QuestionComboBox / Win32 Pin
Fareed Rizkalla11-Apr-10 9:42
Fareed Rizkalla11-Apr-10 9:42 
QuestionC++ class pointers Pin
rupeshkp72811-Apr-10 6:52
rupeshkp72811-Apr-10 6:52 
AnswerRe: C++ class pointers Pin
peterchen11-Apr-10 8:25
peterchen11-Apr-10 8:25 
GeneralRe: C++ class pointers [modified] Pin
Joe Woodbury11-Apr-10 13:52
professionalJoe Woodbury11-Apr-10 13:52 
GeneralRe: C++ class pointers Pin
peterchen12-Apr-10 0:04
peterchen12-Apr-10 0:04 
Maybe I need to clarify: It is not fun to have multiple unmanaged resources as members of a single class.

Reasons:
First, writing the Constructors sucks. When allocating multiple resources in a single constructor, you have to deal with cleanup of a partially constructed class.

(This can be leveraged by some code layout - initializing all to NULL handles before allocating everything, and a separate cleanup function that is called on construciton failure and in the DTor.)

Second, you are encapsulating at the wrong level. When one class holds two file handles you are not only replicating code within the class. The bnext time you write a class holding a file handle, you will again have to implement/block the Default + Copy CTor, DTor and Assignment Operator for a properly behaving class.


In Practice:
I wrote "good practice", not "mandatory" or "the only way to avoid ridicule". I understand that each and every rule puts pressure on the final code. All code can ignore some pressure, good code uses the same constructs to deal with different types of pressure, but at some point the requirements will fight against each other - you can't always satisfy all of them perfectly. We only have cures, not panaceas.

For a large majority of my "need to manage some resource" needs I use CHandleRefT[^] - it isn't perfect, but it works well. The fundamental deviation from OO best practices is to wrap only the resource, not the operations. This weakens encapsulation, but immensely strengthens adoption: supporting a new type of resource is just a few lines without member-to-API forwarding, and the "managed" resource can be used transparently with most native code.
Agh! Reality! My Archnemesis![^]

| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

GeneralRe: C++ class pointers Pin
Joe Woodbury12-Apr-10 6:30
professionalJoe Woodbury12-Apr-10 6:30 
GeneralRe: C++ class pointers Pin
peterchen12-Apr-10 20:27
peterchen12-Apr-10 20:27 
GeneralRe: C++ class pointers Pin
Tim Craig11-Apr-10 17:32
Tim Craig11-Apr-10 17:32 
GeneralRe: C++ class pointers Pin
rupeshkp72811-Apr-10 19:30
rupeshkp72811-Apr-10 19:30 
GeneralRe: C++ class pointers Pin
peterchen12-Apr-10 20:33
peterchen12-Apr-10 20:33 
GeneralRe: C++ class pointers Pin
Tim Craig12-Apr-10 21:00
Tim Craig12-Apr-10 21:00 
AnswerRe: C++ class pointers Pin
Emilio Garavaglia11-Apr-10 20:58
Emilio Garavaglia11-Apr-10 20:58 
QuestionC++Linking Classes/.cpp files to run one after other? Pin
FinalDecap11-Apr-10 5:59
FinalDecap11-Apr-10 5:59 
AnswerRe: C++Linking Classes/.cpp files to run one after other? Pin
Cedric Moonen11-Apr-10 20:57
Cedric Moonen11-Apr-10 20:57 
QuestionArray indexes Pin
Farraj10-Apr-10 23:46
Farraj10-Apr-10 23:46 
AnswerRe: Array indexes Pin
Nelek11-Apr-10 0:26
protectorNelek11-Apr-10 0:26 
AnswerRe: Array indexes Pin
Saurabh.Garg11-Apr-10 2:01
Saurabh.Garg11-Apr-10 2:01 
AnswerRe: Array indexes Pin
Luc Pattyn11-Apr-10 2:15
sitebuilderLuc Pattyn11-Apr-10 2:15 
AnswerRe: Array indexes Pin
Farraj11-Apr-10 3:04
Farraj11-Apr-10 3:04 
GeneralRe: Array indexes Pin
Luc Pattyn11-Apr-10 3:30
sitebuilderLuc Pattyn11-Apr-10 3:30 
GeneralRe: Array indexes Pin
Farraj11-Apr-10 11:27
Farraj11-Apr-10 11:27 
GeneralRe: Array indexes Pin
Luc Pattyn11-Apr-10 11:47
sitebuilderLuc Pattyn11-Apr-10 11:47 

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.