|
ah... in that case, you've moved beyond my sphere of knowledge
|
|
|
|
|
Cool, one of the experts "doesn't know" ;P
|
|
|
|
|
WalderMort wrote: Cool, one of the experts "doesn't know"
Experts are also HUMAN
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I've begun using C++ again after mainly using C# for the past several years. I was wondering what is considered the best way to pass a collection of objects to a function assuming the use of the Standard Template Library.
The function that's receiving the collection doesn't care if the collection is a vector or list or whatever, it just wants the objects. Is there a way to pass a collection that decouples the collection type, e.g vector, list, etc., from the function receiving the collection?
|
|
|
|
|
Leslie Sanford wrote: The function that's receiving the collection doesn't care if the collection is a vector or list or whatever, it just wants the objects.
how does that work ?
|
|
|
|
|
Chris Losinger wrote: how does that work ?
Well, that's kind of the question I'm asking. What I'm doing now is this:
void MyClass::SomeFunction(const std::vector<SomeObject *> *collection)
{
std::vector<SomeObject *>::iterator it;
for(it = collection.begin(); it != collection.end(); it++)
{
}
}
The function just wants to iterate over the collection. If I were in C#, I'd pass an IEnumerable object. The function could iterate over it without having to know what the underlying collection is. I'm not sure how to do the same thing in C++ with the Standard Template Library.
|
|
|
|
|
ah. ok.
then, you would pass two iterators, a start and an end, to your function.
template < class _InIt > void DoStuffToIterators(_InIt _First, _InIt _Last)
{
for (; _First != _Last; ++_First)
DoStuff(*_First);
}
...
DoStuffToIterators(collection.begin(), collection.end();
that's a stripped-down version of STL's "for_each" function.
|
|
|
|
|
Oh, I see! Thanks Chris and Michael for helping me out with this.
|
|
|
|
|
You can model your code after the functions in <algorithm> - pass iterators marking the start and end of the sequence.
|
|
|
|
|
Is it possible to prototype a struct to allow a declaration of that struct in the body of the struct.
typedef struct<br />
{<br />
Node child[4];<br />
}Node;
The above code throws an errors as Node has not been declared. Can Node be used inside the struct Node?
Thanks for any help.
Jim
this thing looks like it was written by an epileptic ferret
Dave Kreskowiak
|
|
|
|
|
jimwawar wrote: The above code throws an errors as Node has not been declared. Can Node be used inside the struct Node?
Thanks for any help.
It is a little easier to do with a class than a struct, but you need a struct name rather than a typedef, and you need pointer references.
<br />
typedef struct stNode<br />
{<br />
struct stNode *child[4];<br />
} Node;<br />
The main reason you want pointer references is that every child would have children, which would have children, which would have children, which would have children, etc. A pointer is a fixed size.
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
If you could define such a structure you'd need a beefy machine to make use of it; it would require an infinite amount of memory!
Steve
|
|
|
|
|
Hi,
In my darkest of hours, you guys never fail to provide shining light. So here goes...
I'm creating a radio button dialog that goes on top of another dialog, and I'm having a problem with initialization of the whole thing. Specifically, when my radio button dialog is being initialized something is firing off an OnBnClicked event for one of the radio buttons.
1) What could possibly be doing this? It happens in such a way as not to actually leave the button selected. (Yet when you step through you definitely get sent to that event handler.)
2) Is it somehow possible using the magic of VisualStudio to trace what's triggering an event handler?
3) As I mentioned, this radio button dialog window gets pasted on top of another dialog window. Is there something weird about this process?
Thanks - eagerly awaiting any replies - Jason
|
|
|
|
|
So if you set a breakpoint in the event handler and check the call stack, what do you see?
In any case, why is this a problem? Is it causing something to not get drawn?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Dave,
Thanks for your interest! It's a problem for the opposite reason, it IS causing something to be drawn. Basically, the rest of the dialog configuration depends on which radio button is selected... so when it automatically selects somethin', it's configuring the dialog according to that. And I'm trying to design it so that it comes up blank, ready for a radio button to be selected.
Here's the first entry in the call stack prior to the event handler:
>Direct3DMFC.exe!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x0012e460, unsigned >int nID=1062, int nCode=0, void (void)* pfn=0x00523fb6, void * >pExtra=0x00000000, unsigned int nSig=56, AFX_CMDHANDLERINFO * >pHandlerInfo=0x00000000) Line 82 C++
Any further suggestions? I should point out that in attempting to debug this little caper i have done the following:
1) Moved the radio button to another location (bug persists)
2) Deleted the message map according to this event handler (bug goes away)
3) Created another radio button with another name for handling similar events. (This one now takes the problem.)
4) Checked as many params etc as I can think of to see why this radio button would be different from all the others? The only thing that I can think of is that it may have been the last one that I put on there.
Yay - debugging!
Thanks again - Jason
|
|
|
|
|
@largeinsd wrote: Basically, the rest of the dialog configuration depends on which radio button is selected... so when it automatically selects somethin', it's configuring the dialog according to that.
Do you have an int member variable that represents all the radio buttons, or do you have a CButton member variable for each?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: Do you have an int member variable that represents all the radio buttons, or do you have a CButton member variable for each?
The former - I have an int variable which is initialzed to -1 (no selection) in the constructor, and then when I get an "OnBnClickedRdBox1" event I do the following:
void CShapeSelect::OnBnClickedRdBox1()<br />
{<br />
m_ShapeSelected = 2;<br />
GetParent()->PostMessage(WM_SHAPE_RADIO_CHANGED);<br />
}
|
|
|
|
|
@largeinsd wrote: The former - I have an int variable...
I would lose that and use a CButton for each. Far less confusing that way.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: I would lose that and use a CButton for each. Far less confusing that way.
Ok, well I'm gonna go ahead and implement that, if nothing else hoping to work around this problem.
But let me ask you this - is there a way to make the dialog class catch the button selection event that is slightly more elegant than they way i've done it here with my own call to PostMessage ?
Thanks - Jason
|
|
|
|
|
@largeinsd wrote: But let me ask you this - is there a way to make the dialog class catch the button selection event that is slightly more elegant than they way i've done it here with my own call to PostMessage ?
Sure. Just use ClassWizard (Ctrl+W) to create a BN_CLICKED handler. All the buttons can use the same handler, or they can each have their own.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Ok, here's the update you're just dying to here:
i encounter the same problem using buttons - as soon as i add an event handler to take action when the radio buttons are selected, it gets activated on initialization. however, using method i can at least use the
m_MyRadioButton.GetChecked()
method to do some screening. it's a fine work around, but where is the ELEGANCE?
|
|
|
|
|
@largeinsd wrote: ...it gets activated on initialization.
The solution to this is to differentiate between initialization and the actual clicking of one of the buttons. I use a bool member variable to denote this.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Also, don't you need a Group Box for the radio buttons to act properly? Is that not true David?
John P.
|
|
|
|
|
jparken wrote: Also, don't you need a Group Box for the radio buttons to act properly? Is that not true David?
Ok, interesting point. For what it's worth I do have the radio buttons enclosed in a group box. If there is some particular initialization required, please let me know.
-Jason
|
|
|
|
|
jparken wrote: Also, don't you need a Group Box for the radio buttons to act properly? Is that not true David?
No, a group control (i.e., a control having the BS_GROUPBOX style) is not necessary. Perhaps you are confusing that with the first radio button of the group having the WS_GROUP style and all others in the group not.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|