|
IIRC, the rules of C++ say that if two functions differ only by const , the non-const version is preferred for a non-const object.
If foo was const , the first assignment would call const int MyClass::operator[](int) const while the second wouldn't compile (the error would probably say something like "'const int' is not an l-value").
C++ doesn't know the context of an array access, so you return a reference to the object to be assigned to if you want to allow assignment. If you want to be able to control assignment (e.g. inspect the value before assigning it), you typically need to return a proxy object from operator[] . The proxy object overloads operator= and is also convertible to the contained type (in this case, you'd probably supply operator int ). You would place your 'setter' logic in operator= .
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
From what I can tell, you only need one of the overridden methods. The second one can be used on either the left or the right side of an assignment statement. Consider this example:
class MyClass
{
public:
MyClass()
{
arr[0] = 2;
arr[1] = 4;
arr[2] = 6;
}
int& operator[](const int i )
{
return arr[i];
}
int arr[3];
};
MyClass foo;
int j = 0,
i = foo[j];
foo[2] = i; The reason has to do with the function returning a reference to arr . It's being treated as if you had written:
MyClass foo;
int j = 0,
i = foo.arr[j];
foo.arr[2] = i;
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
DavidCrow wrote:
From what I can tell, you only need one of the overridden methods. The second one can be used on either the left or the right side of an assignment statement.
Unless you want to call it on a const object to read the value...
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I need to watch for a particular window on the screen. There are two approaches - to use a timer, say with an interval 500 ms, and use the FindWindow() function to check if that window has appeared. Another would be to use a system wide hook to monitor all events, filter out the window create events, and check the caption. Which one would take less system resources.
Is there a way to compare system performance with background processes running?
|
|
|
|
|
Hi all,
I have two views, one of which is associated with a document. this view can be used to open a modal window which performs an operation on some data, and then it is closed. This window creates an array which i need the other view to access; the other view also needs access to the document data!
i can't see how to do this !
can anybody help ?
Paul.
|
|
|
|
|
Hi Everybody !
Could you tell me when it occurs the worst case (n*n) and the best case in quick sort algorithm ?
Thank,
Nguyen
nguyen_nd
|
|
|
|
|
I'd say let professor google answer this ...
anyway, if I remember correctly, it's when the data is already sorted; but I don't remember the justification ...
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
The partition-exchange sort (i.e., quicksort) algorithm performs the worst when the data is already sorted. Its best case is O(N lg N).
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Does anyone know direct download link for
Microsoft Exchange Development Kit (EDK)
I am not able to find it on Microsoft pages.
Thanx
viliam
|
|
|
|
|
|
That is not exactly what I am looking for.
I need EDK (something like SDK)
Header, and Lib files and these are just
tools.
Thanx anyway
viliam
|
|
|
|
|
Hi all,
Where can i find atl service s tutorial for the very beginner?
10'x
|
|
|
|
|
Hi all,
I have an issue related to pocket pc.
I would like to find out how I can delete the most recently opened files or programs in the pocket pc..
I mean I have an uninstallation where I want to remove the icon in the most recently used program group.
thanking you
|
|
|
|
|
I don't have a solution but I think you'll have more replies if you try the Embedded/Mobile forum
|
|
|
|
|
Hi !
I need to retrieve the most significant digit of a number. This must also be rounded to the inferior 'unit'.
Example:
I have 5238.12 and I must have 1000.00
I have 0.02578 and I must have 0.01000
I have 5.45 and I must have 1.00
Any idea ?
Thanks
|
|
|
|
|
Try this code:
d=pow(10,(int)floor(log10(f)));
f=input, d=output.
|
|
|
|
|
Great !
I found the same solution as you at the same time (but your explanation is simpler that mine )
Thanks
|
|
|
|
|
Ok I've found a solution: I can calculate the log10 of the number. I round the solution to integer value. This integer value will give me the number of zero of the number
for 5238.12 the log10 is 3.7192. So I know I have 3 zeros...
Negative numbers will give me the floating point position.
|
|
|
|
|
Cool!!!
MSN Messenger.
prakashnadar@msn.com
Tip of the day of visual C++ IDE.
"We use it before you do! Visual C++ was developed using Visual C++"
|
|
|
|
|
Hi,
I have 25 dialogs who's contols have no access keys or tab order assigned to them.
I was thinking that these two processes are both automatable.
does anyone know of a solution that would auto-tab order and auto-access key my dialogs?
Jeremy Pullicino
C++ Developer
Homepage
|
|
|
|
|
The controls do have a tab order. It's the one defined by which order they appear in, in the dialog template.
Steve S
|
|
|
|
|
I need to use a percentage in a dialog box to see the evolution of a process. Are there any functions for that? I would like to change a text field with the evolution of a Progress Bar. Do you have any idea ?
Thank's for advance
|
|
|
|
|
|
Hi another question... ok I've got CSplitterWnd working perfectly.
I create stuff with CreateStatic, and I wanna pass in a dialog resource (IDD_SOMETHING) designed in the resource editor to the view with CreateView... So how do I pass in CFormView into CreateView?
CreateView(0, 0, RUNTIME_CLASS(CFormView), CSize(0, 0), &ccontext);
does not work... or do i need to derive a class from CFormView and pass it in instead? please help... thanks in adv...
|
|
|
|
|
Yes, you should derive a class from CFormView . Remember to use DECLARE_DYNCREATE in the class declaration and IMPLEMENT_DYNCREATE for the class in a source file.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|