|
The common mistake in this case (writing if (x = 1) instead of if (x == 1) ) is I believe a simple confusion caused by the fact that people often write assignments and the equality sign is a common operator used in mathematics, real life etc. whilst "==" does not exist. So nothing's wrong with making such a mistake and warning level 4 will put you right on the line, BUT:
Michael Dunn wrote:
Put the constant on the left side: if (1 == x)
This is something what I would call nonsense. Applying in a real-life situation:
You are making an equality comparison, and thinking to yourself as suggested:
- What am I doing?
- Checking for equality.
- So I should write an if..
- Yes exactly, do it. Stop! Put the constant on the left side!
- Why? Why should I do that?
- Because you can mistype the equality operator..
- Oh hell yeah I could've mistyped it, so I put the constant to the left..
- Yes and remember that everytime you make a comparison.
- Sure I'll remember.
The point is (just in case): Writing 1 = x; is invalid as an assignment so in order to remember to put the constant on the left side, one must keep in mind that (s)he is making a comparison. So, if you are aware of the fact that you're making a comparison, then there's no chance (besides a brain tumor or some similar disability) that you'll be writing "=" instead of "==" accidently.
I must say that I've seen this advice too many times and believe that people never think about it actually, just forward the idea.
Or worse: I have a tumor of a different kind
|
|
|
|
|
Not exactly a programming error but I would submit our database. Hundreds and hundreds of tables, dynamic SQL *everywhere*, if anything is documented it's months and months out of date.
As for becoming a serial killer, try this[^]...
not having a good day...
You should save yourself and your company years of grief by shooting yourself through the head immediately. Believe me, in the long run it'll turn out better for everyone. - Tyto (at arstechnica)
Awasu 1.0[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
That's EASY!
(Note improper placing of ';')
<br />
for (i = 0; i < MAX; i++); <br />
{<br />
}<br />
OR coming from a Fortran background...
<br />
for (i = 1; i < MAX; i++)<br />
{<br />
Array[i] = something * something_else;<br />
}<br />
where I have forgotten that arrays are indexed by 0 in C++
|
|
|
|
|
Guys ,u have forgotten about pointers!!!!
int i;
int *j=&i;
int k;
for(count=0;count
|
|
|
|
|
Try something like this:
short n;
scanf("%d", &n);
Gives amazing unpredictable results. If n is the first declared variable in the function, this code can overwrite function return address.
|
|
|
|
|
Ya know, I have this extensive list of amazingly dumb mistakes that I was going to post,...but, compared to me ,you guys look like Nobel Prize winners. (Yes, I have made that assignment operator mistake a number of times.)
My all-time most embarrassing errors:
I ALWAYS forget, #include "resource.h" at the beginning of the source file. ...and, I ALMOST ALWAYS make a number of typographical errors, so, I get the "Undeclared Identifier" error message ALL THE TIME (...it's like an old friend now).
Most frustrating: It took me forever to figure out that you must use TWO backslashes when designating the "file path string" with either CreateFile or the iostream library class functions (and I would have NEVER figured that out if I hadn't stumbled upon it in an introductory book on C++, completely by accident).
|
|
|
|
|
In msdn i see two possible ways to convert an MFC CString Object to a C-Style null terminated string:
1) cast the CString to an (LPCTSTR)
2) call CString::GetBuffer() on the object.
Is one method more correct or safe than the other? For example, if i wish to call "stdio.h" library functions like strcmp that take char*'s as parameters, which method should I use to convert my CStrings to Char*'s?
Thanks
Ben
|
|
|
|
|
The LPCTSTR operator returns a constant pointer to the underlying null-terminated string - i.e. you can't change it. It is invoked implicitly by the compiler when a function requires a constant string. GetBuffer() returns a non-const pointer to the underlying string, which you must release when you are finished by calling ReleaseBuffer().
So you can normally just use the LPCTSTR operator unless you need to change the string.
Dave
|
|
|
|
|
|
|
|
How do I get a pointer to the view from doc in an SDI app?
Basically I need to acces the Device context in view from DOC ...
Thanks
|
|
|
|
|
For an SDI app, you should be able to do:
CView* pView = pDoc->GetNextView( pDoc->GetFirstViewPosition() );
Dave
|
|
|
|
|
I was trying to do that inside a Doc ...
I m able to get a pointer to the view from the CMyApp
but not from CMyDoc..
what Im trying to do is to to access the pointer to the "device context" of the MyView(CFormView) from MyDoc (CDocument) ...
|
|
|
|
|
Loooser wrote:
I was trying to do that inside a Doc ...
I m able to get a pointer to the view from the CMyApp
but not from CMyDoc..
Do you mean that the GetNextView( GetFirstViewPosition() ) method calls i mentionned above failed, or that you couldn't find a member pointer to the view (or a function like GetActiveView())?
Loooser wrote:
what Im trying to do is to to access the pointer to the "device context" of the MyView(CFormView) from MyDoc (CDocument) ...
Unless you are currently drawing the view, it probably has no associated device context. But you can create it easily enough by:
CClientDC dc( pView );
This creates a client DC - a device context that allows you to draw to the client area of the view.
Dave
|
|
|
|
|
Your method calls failed,
Unless Imdoing something...here is what I have...
CMyDoc* pDoc = GetDocument();
CMyView* pView = pDoc->GetNextView(&pDoc->GetFirstViewPosition());
and here is the error message
error C2102: '&' requires l-value
|
|
|
|
|
Why do you have the ampersand (&) before the call to GetFirstViewPosition()? That will pass the address of the POSITION to the GetNextView() method, not the POSITION itself. Remove the ampersand and it should work.
Dave
|
|
|
|
|
Tried that ....no luck!
|
|
|
|
|
CMyDoc* pDoc = GetDocument();
CMyView* pView = pDoc->GetNextView(pDoc->GetFirstViewPosition());
|
|
|
|
|
CMyDoc* pDoc = GetDocument();
CMyView* pView = (CMyView *)pDoc->GetNextView(pDoc->GetFirstViewPosition());
|
|
|
|
|
Brian
tried this one well...still no luck
here is the Error Message...
error C2664: 'GetNextView' : cannot convert parameter 1 from 'struct __POSITION *' to 'struct __POSITION *& '
A reference that is not to 'const' cannot be bound to a non-lvalue
|
|
|
|
|
Ah - it wants a reference.
Try:
POSITION pos = pDoc->GetFirstViewPosition();
CView* pView = pDoc->GetNextView( pos );
It wants to update the position, even though in this case we know that there is only one...
Dave
|
|
|
|
|
Sopmething is very wrong with your design if you need to do that. The document is not responsible for displaying data; the view is.
|
|
|
|
|
well im trying to get my doc to update the view every time I change the data in my doc.
I need to draw something ont he view based on the data from my doc.
I thought I;d get a pointer to the Device context of the view and access it through my DOC and do the drawing.
Any ideas howelse this can be done?
thanks
|
|
|
|
|
Loooser wrote:
I need to draw something ont he view based on the data from my doc.
That what the view is doing: Showing an aspect ('a view') of the documents data.
That is, the view takes the data it wants to use from the document and displays it.
Loooser wrote:
im trying to get my doc to update the view every time I change the data in my doc
Try to look into UpdateAllViews() . It is exactly what you want.
My opinions may have changed, but not the fact that I am right.
|
|
|
|