|
You should find a project which you would like to use but is not exactly what you need. This way you will have motivation as well as new features which you can add to the system.
-Saurabh
|
|
|
|
|
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?
|
|
|
|
|
Take a look at where you added this case entry to make sure it is not falling through the loop. Also review the function/block where you fill your columns.
It's time for a new signature.
|
|
|
|
|
Function/block is fine, after removing WM_NOTIFY everything returned as normal.
How can it be falling through the loop?
|
|
|
|
|
Fareed Rizkalla wrote: How can it be falling through the loop?
Not having Superman vision I have no idea. Try posting an extract of your code so people can see what you have done/are trying to do.
It's time for a new signature.
|
|
|
|
|
(1) is the case WM_NOTIFY followed by a break?
(2) are the WM_NOTIFY messages forwarded to the base / default window proc?
|
|
|
|
|
What parameters do I need to specify in order for the ComboBox to accept more text than the visible area has to offer?
|
|
|
|
|
Suppose a class in C++ has a pointer in it.
class
{
int*x;
}
Then what all precautions we need to take regarding handling the pointer?
|
|
|
|
|
There are two ways of dealing with pointer members:
Default Constructor
e.g. initialize to NULL or allocate memory
Destructor
free memory if allocated and the class "owns" the memory
Copy Constructor & Assignment Operator
Handle them correctly - e.g. free the memory allocated
it is good practice to use one class per unmanaged resource.
|
|
|
|
|
peterchen wrote:
it is good practice to use one class per unmanaged resource.
Since when?
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.
modified on Sunday, April 11, 2010 8:02 PM
|
|
|
|
|
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.
|
|
|
|
|
peterchen wrote: 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.)
peterchen wrote: 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++.)
|
|
|
|
|
Joe Woodbury wrote: 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".
Do you roll dice?
|
|
|
|
|
peterchen wrote: it is good practice to use one class per unmanaged resource.
Unmanaged? Managing them is the whole point of C++. I think you must be a C# programmer who lost his way.
You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.
|
|
|
|
|
Thanks PtereChen, Joe and Tim for the inputs
|
|
|
|
|
You prefer the term "raw"?
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.
|
|
|
|
|
peterchen wrote: You prefer the term "raw"?
I don't need any qualifiers but "managed" seems to be how Microsoft touts all the .NET BS.
peterchen wrote: You, OTOH must be a C++ programmer who thinks harder is better.
I'm a C++ programmer who doesn't think C# brings anything worthwhile to the party. Just another of Microsoft's little proprietary ploys. Oh, look what Sun did with Java, I bet we can do that, too.
You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.
|
|
|
|
|
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:
| no copy | shallow
| deep | no ownership | use dumb pointers and disable copy and assing | use dumb pointer and don't care | NO SENSE | exclusive | delete on destruction, disable copy/assign | NO SENSE | implement copy and assign to create resource copies | pass-through | NO SENSE | use auto_ptr or implement equivalent semantic (transef
the pointer to the copy and make the original pointer null) | NO SENSE | sharing | NO SENSE | use reference counting or a reference counting capable
pointer (like shared_ptr) | NO SENSE |
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.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
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:
# Virus Scanner
scanner.out : ProgramList.o MD5Hash.o HazardCheck.o
g++ -o scanner.out ProgramList.o MD5Hash.o HazardCheck.o /usr/lib/libstdc++.so.5 CONFIG/rudeconfig.lib HASH/src/libhl++.a
ProgramList.o: PROGRAMS/ProgramList.cpp
g++ -c PROGRAMS/ProgramList.cpp
MD5Hash.o: HASH/MD5Hash.cpp
g++ -c HASH/MD5Hash.cpp
HazardCheck.o: CONFIG/HazardCheck.cpp CONFIG/hazard.conf
g++ -c CONFIG/HazardCheck.cpp
clean:
rm *.o scanner.out
#END OF MAKE FILE
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.
Any/All help is appreciated.
|
|
|
|
|
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.
|
|
|
|
|
i need help with writing a c code
lets say i have an array[5]
i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one.
example
{2,5,1,8,4}
my new array of indexes will be (counting from ZERO)
{3,1,4,0,2}
i though of sorting it and save the indexes in a temporary array or something but it didnt work
any ideas how can i solve this ?
|
|
|
|
|
Mmmm.... it looks like homework.
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.
Regards.
--------
M.D.V.
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.
|
|
|
|
|
Here is an approach:
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.
-Saurabh
|
|
|
|
|
Hi,
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.
|
|
|
|
|
thanks for your replies guys i appreciate it
ive tried this code.. it may look bad lol
but i'll appreciate it a lot if someone can tell me how to correct it
int a[] = {2,1,5,4,7};
int index[] = {0,1,2,3,4};
int i,j,temp;
for(int i=0; i<5; i++)
{
for(int j=0; j<5-1; j++)
{
if(a[index[j]]>a[index[j+1]])
{
temp = index[a[j+1]];
index[a[j+1]] = index[a[j]];
index[a[j]] = temp;
}
}
}
thanks
|
|
|
|