|
I have spent some time trying to write code to send a reference to the vector element. I think I might have had trouble extracting this from the iterator. I must have somehow persuaded myself that there was some reason as to why passing a reference to a vector element from an iterator was a bad idea. I was just wondering if anyone knew if there is some sort of trap here - . I would probably be happier doing it this way if it is straightforward and safe. It seems more elegant. I don't really want to get bogged down in making the changes, though, if there is some inherent problem.
The functions in the iterator block use and possibly change the data in the vector element but don't call any further functions that use the vector element. There is only a single thread accessing the vector.
|
|
|
|
|
You didn't use an actual iterator anywhere in the code that you posted, so I don't see the problem. When a function argument is declared as a reference, you can just pass a vector element, such as sliderVector[id] . If you do have an iterator, then you can just dereference it to get the vector element and pass that instead. If you wish to pass a pointer instead, just use the 'address-of' operator:
void bar_ref(double& element) {
element *= 2.0;
}
void bar_ptr(double* p_element) {
*p_element += 3.0;
}
void foo() {
std::vector<double> myvec(2); myvec[0] = 3.1415;
myvec[1] = 2.718;
for (std::size_t i = 0; i < myvec.size(); ++i) {
bar_ref(myvec[i]); bar_ptr(&(myvec[i])); }
for (auto it = myvec.begin(); it != myvec.end(); ++it) {
bar_ref(*it); bar_ptr(&(*it)); }
}
The only possible trap I see is trying to pass an iterator directly when a pointer is expected. An iterator may behave like a pointer, but, while - depending on the implementation - it may actually auto-convert to a valid pointer (I know VS 2003 did that), you shouldn't rely on that. (e. g. VS2010 doesn't accept it)
|
|
|
|
|
Thanks for this! I will certainly give this a go and let you know how I get on. It might not be for a few weeks, though...
|
|
|
|
|
Thanks very much for showing me how this can be done, Stefan. I have tried your method and it works perfectly.
I have thought some more about what I'm trying to achieve, though, and have come to the conclusion that since the vector array has class scope and that its members don't themselves know, or need to know, whether they are the current item in the 'for' block (where I've got the iterator) - the more elegant solution would, I guess, involve just sending an (integer) identifier for the required element.
Your help is very much appreciated. Your correspondence has helped my thought process a lot.
Cheers,
Ben
|
|
|
|
|
Glad I could be of help.
As for your conclusion, keep in mind that sending an ID of the element requires you to also pass the container that this ID refers to! Otherwise the function won't know what to do with that ID. Whether that ID is an iterator or index value doesn't matter in this regard - neither have an inherent knowledge of the container they refer to.
If the container is class scope, and you pass an ID to a class member function, that would work. But from a design view, doing so would still be questionable as the function actually shouldn't need to care about the rest of the container - you have introduced an unnecessary dependency. If at some point you realize you want to use that same function for elements not related to this class, you cannot easily do it, due to that dependency.
|
|
|
|
|
hi all I wonder how comfigurar the code blocks for borland ide bcc55 and download the library QTK and c + + qt hope your help.
|
|
|
|
|
Get a time machine.
Dial up 1996.
Ask again when you get there.
Seriously, Borland C++ 5.5 is so out of date it's not worth thinking about. (But I loved it in its day.) There are much better modern compilers available for free.
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Ha Ha. How can you reply like this
|
|
|
|
|
i have installed c++ program ,and this program is failed
to install and error message showed{unable to load development environment dll}
note
i have installed dll-files
and i have tried ,the problem is founed.
please help me
|
|
|
|
|
emanalshboul wrote: ...the problem is founed. You found the problem?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
You need to provide some more information on the problem. Is that the exact message that you see, and which DLL is it missing?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi all,
i make a nice CMyFileDialog derived from CFileDialog that works fine for my need, but i need just one function for my Dialog and this is when a user select some file in a list to catch this event, something like:
"OnItemChange", is this possible, something like CListCtrl function itemchange??? If yes can someone give me a suggestion how to do this?
Best Regards
bosfan
|
|
|
|
|
Maybe this[^]thread may help.
|
|
|
|
|
Hi,
thanks for answer, but in this example works only if you know the id of a list control, e.g. my own list controls on a dialog where i give the id to the ctrl? Right?
Best Regards
bosfan
|
|
|
|
|
Check out the lpfnHook member.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Hello,
this is what i'm looking for, this works.
I found a example with OnNotify() and i use this as reference:
ImageImport Filedialog
Many thanks
Best wishes
Mirsad
modified 9-Oct-12 9:14am.
|
|
|
|
|
This [^] might help. Also just search in Codeproject you can find many!
-Sarath.
Rate the answers and close your posts if it's answered
|
|
|
|
|
Hi,
thanks for answer
bosfan
|
|
|
|
|
Hi,
I am using CStringArray to store images in sequence. I need one help. I will load an image on the canvas area, then I'll apply an effect on that image. Then I'll store that image into CStringArray. I'll store the images in sequence as I'll apply an effect one after one. Now to undo the last applied effect I'll get the last image and load the same into canvas Area.
Isuue is that I am using check boxes to apply effects on images. If user has applied effect 'A', then 'B', then 'C', and so on. Now if user has unchecked the box with effect 'B', then It should load the image which was before applying effect 'B'. I want to store and know the state of the image before applying the effect and load that image only.
Anybody have any idea how to do this. I would appreciate if anyone can suggest me a better solution than what I am using.
Regards,
Mbatra
|
|
|
|
|
HINT:
map<int,CString>, use this to create association between index and CString object.
You talk about Being HUMAN. I have it in my name
AnsHUMAN
|
|
|
|
|
mbatra31 wrote: I am using CStringArray to store images You may find that CByteArray [^] would be a better choice.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi, this is my first post.
Work flow is like this:
i open child dialog from a dialog( which is launched from a toolbar of a window) after editing i close the child dialog, after closing child dialog shadow is retained on window but not on the parent dialog. Later when i close the parent dialog the child dialog shadow disappears.
(issue exist only when i edit in child dialog)
Thanks a lot,
Shivaraj
|
|
|
|
|
Can you post somecode, it will be helpful to get help..
|
|
|
|
|
Thanks for reply, I have seen same design patterns(using child dialog)in the same application but there is no issue there. I compared the code, am not finding any difference in RePainting aspects.
here i have sample code..
Below used OnModifyview dialog is child dialog of a dialog.
void CSelectColumnViewDlg::OnModifyview()
{
int iIndex = m_ctrlColumnViewList.GetCurSel();
if (iIndex < 0)
return;
double dViewId = m_ctrlColumnViewList.GetItemDataDouble(iIndex);
if (dViewId == 0.0)
return;
CWaitCursor wait;
CComPtr pDispColumnView;
CComPtr pManager;
pManager.CoCreateInstance(CLSID_TrackingDataManager);
if (pManager)
pManager->GetColumnView(dViewId, &pDispColumnView);
CComQIPtr pColumnView(pDispColumnView);
if (pColumnView == NULL)
return;
CModifyColumnViewDlg dlg;
dlg.SetColumnView(pColumnView);
dlg.SetType(m_lSolution, m_lListType);
dlg.SetTrackGroupCd(m_dTrackGroupCd);
std::set* pExistingNames = GetExistingNames(iIndex);
dlg.SetExistingNames(pExistingNames);
if (dlg.DoModal() == IDOK)
{
HWND hWndAncestor = ::GetAncestor(GetSafeHwnd(), GA_ROOT);
m_ctrlColumnViewList.DeleteString(iIndex);
CComBSTR bstrName;
pColumnView->get_Name(&bstrName);
int iIndex = m_ctrlColumnViewList.AddString(CString(bstrName));
m_ctrlColumnViewList.SetItemDataDouble(iIndex, dViewId);
m_ctrlColumnViewList.SetCurSel(iIndex);
}
if (pExistingNames)
delete pExistingNames;
Invalidate();
//OnPaint(); //not working
//RedrawWindow();// not working
}
|
|
|
|
|
Are you using any drawing in the parent dialog that launched the CModifyColumnViewDlg?
If you don't have any OnPaint handler for the parent dialog, Invalidate is not required.
Otherwise use UpdateWindow()
|
|
|
|