Thanks for the link. Here are the relevant portions: CListCtrl& GetListCtrl( ) const; CListCtrl& theCtrl = GetListCtrl();
I'll ignore the "const" modifier for now, as its use confuses me at the moment. Here's a paraphrase of what you mentioned, as I understand it: 1) Using "type&" is sort of like using a pointer ("type*") 2) By using "type&", a test for NULL is not needed 3) When using "type&" instead of "type*", you access the object's fields by using "." instead of "->" Before I go on, please understand that I come from a Java background, and my intent is to learn (re-learn) ANSI C -- thus my confusion with pointer- and address-related semantics in C. I have no interest in learning C++ right now, but it's definitely in my queue -- everything I refer to will be with respect to ANSI C. My intent is to take what I learn from C and use that knowledge to write Java programs that access C native code via JNI. I have a quite a few C books lying around here, including: a) ANSI C spec b) comp.lang.c FAQ list c) Rationale for International Standard -- Programming Languages -- C d) The New C Standard -- An Economic and Cultural Commentary e) The C Programming Language f) C in a Nutshell I'm currently jumping around between a, b, e, and f (mostly concentrating on f right now). For my C environment, I use Eclipse 3.3 with CDT and the gcc from cygwin on WinXP Pro. Now, back to C... >From the paraphrase, I have some questions... 1) Since "type&" is sort of like "type*", does this mean that I can cast between the two types? For example: CListCtrl& var1 = GetListCntl(); ...might be rewritten as... CListCtrl* var1 = GetListCtrl(); ...or explicitly cast as: CListCtrl* var1 = (CListCtrl*) GetListCtrl(); 2) Let's assume that NULL is represented as integer 0. If it's not necessary to check for NULL when using "type&", then what happens in the following scenario if GetListCtrl() returns NULL? CListCtrl& var1 = GetListCntl(); //GetListCtrl returns NULL (ie, 0x0) println("%d", var1.x); //assume that var1 represents an object with a field named "x" which we are accessing My guess: I think that evaluating var1.x would attempt to access the value stored at field x from an object (ie, from the var1 obj) whose address starts at 0x0... probably resulting in some kind of exception (I haven't tried this yet).... but it probably wouldn't be a NullPointerException since I don't think that 0x0 would be interpreted as a NULL value, due to using "CListCtrl&" as the type instead of "CListCtrl*". 3) In ANSI C, what is meant by an "object"? According to the "C in a Nutshell" book: "the term _object_ refers to a location in memory whose contents can represent values. Objects that have names are also called _variables_.: Okay, so it sounds like in ANSI C, an object can be a primitive like a var of type "int", right? Or, must that int type be wrapped in a struct in order to be considered an object? The "." operator "dereferences an object" (not sure if I have the terminology right) so that a field or function of the object can be accessed, right? I don't recall ever being able to use the "." operator on a var of primitive type (not that it would be necessary, since primitives are not composite types...). So, I'll assume for now that in C, the term "object" refers to a non-primitive type. A "variable" is thus the object's name, and represents the entire object (as opposed to its starting addr). The _address_ of the variable is the location in memory at which the object starts (assuming that the object is a contiguous collection of one or more memory slots). You get the addr by taking "&var1". So if var1 is of type int, then &var1 represents the starting address of the object whose size and type are determined from the int type. Let's say that var2 is declared like this: CListCtrl& var2; Here, I'm not sure how to parse-out the type from this statement. The variable name is var2, so the type must be what's left, which is "CListCtrl&", right? I don't think that the var would be "&var1" and its type be "CListCtrl"... So confused... I appreciate any help on this, including any links/articles that might help clarify this confusion. Regards, Will ----- Original Message ---- From: Thomas Hruska <[EMAIL PROTECTED]> To: c-prog@yahoogroups.com Sent: Friday, August 31, 2007 10:46:58 PM Subject: Re: [c-prog] MSVC Compiler optimized away variable = Confusion while Debugging? Will McQueen wrote: > Hi, > > I'm looking at this code, and I have a quesiton about this statement: > > > const SomeClass & crSomeThing = (bIsValid) ? SomeClassObjA : > SomeClassObjB; > > Assignment requires an lvalue, no? Where is the lvalue? I see the > modifier (const), the type (SomeClass), and then I expect to see a > variable name but instead I see an address-of operator (&). How is > "&crSomeThing" a valid variable name in this variable declaration > statement? > > Thanks, Will You can do that. Basically, crSomeThing and SomeClassObjA are identical (i.e. both 'point' at the same location - or at least crSomeThing points at SomeClassObjA) . Same applies for SomeClassObjB (if that is picked). Useful for performance reasons because you aren't copying the objects. Also reduces the number of lines of code/complexity of the logic. Sort of like a pointer but don't have to do the usual tests for NULL before using it (and you can use '.' instead of '->'). You'll see this used often in MFC. For example: http://msdn2. microsoft. com/en-us/ library/ayyh1555(VS.80).aspx -- Thomas Hruska CubicleSoft President Ph: 517-803-4197 *NEW* MyTaskFocus 1.1 Get on task. Stay on task. http://www.CubicleS oft.com/MyTaskFo cus/ ____________________________________________________________________________________Ready for the edge of your seat? Check out tonight's top picks on Yahoo! TV. http://tv.yahoo.com/ [Non-text portions of this message have been removed]