Will McQueen wrote:
> 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.
'const' modifiers to the end of a member function declaration say to the
compiler "I won't be modifying any of 'this' class' variables inside
this function" (exception: Variables which have 'mutable').
GetListCtrl(), in this case, is probably written* like:
CListCtrl &CListView::GetListCtrl() const
{
return m_ListCtrl;
}
Where m_ListCtrl is probably declared as a member variable:
...
CListCtrl m_ListCtrl;
...
* I'm just a tad lazy at the moment to go hunting down the actual source
code.
> 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.
You may find C++ to be a more natural transition...
> 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();
You can't use MFC with C. GetListCtrl() is inside a class.
You may be able to cast it to a pointer like this:
CListCtrl *var1 = &GetListCtrl();
...but why would you want to?
> 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 = GetListCtrl(); //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*".
If the object hasn't been allocated, then you could run into a crash BUT
it would occur inside GetListCtrl() when it attempts to dereference the
pointer inside the class. (In debug mode, MFC has ASSERT() sorts of
statements all over the place with comments on what to do to fix various
problems, but see above for how GetListCtrl() is probably written).
> 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"...
My definition of 'object' is "anything that has been encapsulated into a
cohesive unit and carries all functionality with that unit necessary to
do work". So, primitives (int's, char's, etc.), classes, and template
instances all apply.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/