it is good practice to use one class per unmanaged resource.
I know it sounds good in theory and even I got carried away at first. Since they, I've discovered that encapsulating functionality is far more useful, even if that means you have a few handles and pointers to maintain. I have a specific class that I've done both ways; the latter is far more elegant and easier to understand/debug/maintain.
Maybe I need to clarify: It is not fun to have multiple unmanaged resources as members of a single class.
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.
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.
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.
If you choose to have copy constructors and assignment operators, you still have to write them with a class full of encapsulated objects.
The alternative is to block copy constructors and assignment operators, which is quite easy. I have quite a few classes where I do that.
(For the record, I've encapsulated a file handle and synchronization objects, though I don't always use them for various reasons; I make a decision on a case-by-case basis, based on what is best for that class, not on any rules.)
you have to deal with cleanup of a partially constructed class.
I can't remember the last time I even worried about that; I just write my code such that it's irrelevant AND so that a class always constructs. (I don't do this in C#, but I accept slow and bloated in C#, but not in C++.)
I make a decision on a case-by-case basis, based on what is best for that class
That fits my bill of the "good" in "good practice". Sometimes the pieces come together, sometimes they don't. I see "good practice" ostly as guidance when you don't know how to tell what's "best for the class".
I am not writing C++ because I enjoyx managing resources.
When I add a member, I think of the member, not it's contents. a int * doesn't clean up after itself, a scoped_ptr<int> does. We could call them "potty trained", too.
Tim Craig wrote:
I think you must be a C# programmer who lost his way
You, OTOH must be a C++ programmer who thinks harder is better.
There is no unique answer, since it mostly depend on the semantics your class intended to be used and on the semantic of the resource (memory is a particular cas of resource, but having an HANDLE instead of an int* doesn't change the nature of your question) is planned to be represented.
The first thing to decide is what should happen when an instance of your class is assigned to another. Should both the classes refer to the same resource (that is: whatever modification required from both will act on the same object) or should them refer to different resources (that is: you need shallow or deep copy)?
Or should one instance represent one resource, hence no copy/assignment can be done and your classes must always pass by reference?)
The second thing to decide is the kind of "ownership" your class has on the resource: Does it own it (has the right to destroy it?) Is it an exclusive owner? Can it pass the ownership to somebody else? Should it share the ownership with other copies?
The following table may help:
use dumb pointers and disable copy and assing
use dumb pointer and don't care
delete on destruction, disable copy/assign
implement copy and assign to create resource copies
use auto_ptr or implement equivalent semantic (transef
the pointer to the copy and make the original pointer null)
use reference counting or a reference counting capable
pointer (like shared_ptr)
A third thing to decide is how your class acquire the resource: does it create it? Does it get it from a manages? is it created outside and passed to be handled?
As you see, there are many choices, some optimal in certain cases, some optimal in other.
This is probably a really stupid thing to ask considering the development I'm doing (effectivly creating a virus scanner), but how do I link classes/cpp files?
I have 3 applications/sections that I can compile/combine with a makefile, that's fine, but I need them to run 1, 2, 3 once the output from the makefile is done.
Currently the only section to actually run is whichever I have "main" in and obviously if I put that into all three, they won't compile as one.
I've been looking all over the place at all sorts, header files and such, but there is no mention of how to actually do this although I'm sure it must be possible. I'm used to being able to do this in Java and I'm sure I've seen C++ applications do it, but not worked out how.
I have 3x .cpp files which are combined into one using a makefile:
scanner.out will run, but only whichever .cpp has "main()" in it, so the other two are defunct but are included within scanner.out. If I compile the three files separately I end up with three programs, but I need them to work together as a single application.
I'm doing all my work on Linux using vi to edit the files, but I assume what I want to do is generic between systems.
You really need to read some books on C++ (from the basics). An executable has only one entry point (the main function in your case, to make it simple). Separating your code into different cpp files is useful only for structuring your code properly, but it doesn't mean that you have 3 "programs". You still have only one main. If you want to execute code from the different files, you need to call these functions from within your main function.
What about if there are two identical numbers in different positions? Is that possible?
Anyways... one possibility (hard coded) could be... you go through the array evaluating the actual number with a maximum that you update when you find that the actual number is higher than this max. If you find it, you can save the value of "i" temporary and continue evaluating. When the array is checked to the end you can know where the highest number was with your temp_i, then put a very low (or negative) number that is not going to disturb you on the next evaluation and start over again.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
Create another array where each element stores (v, i), where value is the value in the input array and i is its index. Then you sort this array on v. You can store v and i in a structure and then use qSort for sorting.
the normal approach to sort an array value without moving its content is this:
- create an index array, initialized to 0...N-1
- implement whatever sort algorithm you like on that array, except, rather than comparing index[i] and index[j], compare value[index[i]] and value[index[j]] and move the content of index around as a result of the sorting steps.
another thing.. you've mentioned something about initializing the array from 0->N-1, ive done that while i know what size my array is.
what if the size is unknown N, how can i initialize it to be from 0 to N-1? should i make another loop to initialize it or there is some other way?