Re: Newbie static class member question [C++]
From: Gary Labowitz (glabowitz_at_comcast.net)
Date: 04/17/04
- Next message: Gary Labowitz: "Re: Newbie static class member question [C++]"
- Previous message: Petec: "Re: multi-dimensional arrays of char"
- In reply to: Ryan Stewart: "Re: Newbie static class member question [C++]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 17 Apr 2004 09:58:29 -0400
"Ryan Stewart" <zzanNOtozz@gSPAMo.com> wrote in message
news:qKqdnemmusg_Bx3dRVn-uQ@texas.net...
> "Leor Zolman" <leor@bdsoft.com> wrote in message
> news:eo0180p73g7phm3v6qqavprr6uc9durq5i@4ax.com...
> > On Fri, 16 Apr 2004 19:24:51 -0500, "Ryan Stewart"
<zzanNOtozz@gSPAMo.com>
> > wrote:
<<snip>>
Still not perfect, I know, since private and public members are
> listed, and since it's not really for showing the interface. I'm
running
> myself in circles now, so forget it. The class declaration (if
that's the
> correct term) just *is*, and you declare all the class members
within the
> class declaration, but define them outside of it (as a general
rule).
<<huge snip>>
Think of it this way: A declaration is used to tell the compiler the
meaning of an identifier. This does not cause allocation of any
memory. Since a class declaration it is just a "blueprint" of what
will be when and if an object of that type is defined, it may be used
in any number of translation units. They are each just looking at the
blueprint so that they know names and offsets to the various parts of
the class. Typically, these blueprints are in headers and included in
any TU that is going to reference any of the data or functions of a
class.
The definition tells the compiler to generate code to allocate memory
for an identifier. The identifier is the source code name for the
address of the memory allocated. All identifiers in C++ are in some
namespace, which I like to think of as a dictionary that tells what
that identifier means. One such namespace is global, in that it has no
name, and is exactly what old-fashioned global meant. You specify that
namespace, that is to say the global dictionary (when you have to), by
using the scope resolution operator with no name! Example: To refer to
an identifier XXX that you didn't declare in a named namespace you use
::XXX. You only need the sro when there are two identifiers with the
same character string as a name.
The automatic search for a "naked" identifier always starts in the nam
espace of the nearest block (delimited by French brackets) and
proceeds up levels of imbeddedness. The usual way of thinking of these
is to name the scopes based on where the identifier is declared. From
bottom to top they are local (which includes any block that is not
named), argument (which are local, but declared in the function
header), file (which are outside any functions, but in the TU),
external (which are identified with the keyword extern and are
actually outside of the TU but will be in the linked module).
Note that prototypes and forward references are actually declarations
with specialized names.
The complexity comes with the fact that a declaration can also be a
definition. The normal int x; both declared the identifier x (and
places it in some namespace, depending on the structure of the program
at that point) and defines an area in memory for the int-value of x to
be stored. Notice that if it is desired to select the namespace the
programmer can specify it when declaring/defining: int namespace::x.
This is what is done when d/ding a static variable of a class. In the
declaration of the class it causes no memory allocation. It is the
definition of int classname::X that actually allocates the memory.
Not at all like Java, is it?
[Note: off the top of my head, so feel free to pick at this and
correct anything that needs correcting.]
-- Gary
- Next message: Gary Labowitz: "Re: Newbie static class member question [C++]"
- Previous message: Petec: "Re: multi-dimensional arrays of char"
- In reply to: Ryan Stewart: "Re: Newbie static class member question [C++]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|