It's not CWinApp that's special, the "specialness" is at a lower level. When a process starts its initial thread calls WinMain[^]. This thread can create as many threads as it likes and they in turn can do the same, but as soon as the the initial thread exits the process is history. MFC is built on top of Windows, and the initial thread is the one that's generally going to be executing CWinApp's methods. So yes, a process does need at least one thread to be executing: the initial one.
I am a college student, looking to participate in an open source project. The only experience I have is class work, and I want to start participating in "real world" programming. I was wondering if anyone could point me in the right direction to find a project I might be able to help with. Thank you for any advice you can give.
While working with ListView I started adding case WM_NOTIFY to the parent window message loop.
Although it was empty, but when I ran the app all the columns that used to be populated are now empty!
What kind of problem could I be looking at?
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.