What does line 162 do ?

R

Rafael Anschau

159: static PartsList GlobalPartsList;
160: };
161:
162: PartsList PartsList::GlobalPartsList;

I would understand if it initialized the object
GlobalPartsList with some value, but as it is now, it seems to me
without purpose.

Thanks,

Rafael
 
R

Rafael Anschau

Yes, my text says it initializes or defines it there. That line 159
only declares it.
But initializes it, defines it with what ? What it does is to make it
possible to later assign
a value ?

[]´s

Rafael
 
J

Jim Langston

Rafael said:
Yes, my text says it initializes or defines it there. That line 159
only declares it.
But initializes it, defines it with what ? What it does is to make it
possible to later assign
a value ?

[]´s

Please do not top post. Message rearranged.

Since GlobalPartsList is static, there can be only one in the program.
There can be only one definition.

Line 162 is simply *the* definition. Line 159 is the declaration. You can
not define a static variable inside a class declaration, you can only
declare it. So outside the class declaration you need the definition, which
you have.

It may make more sense if it was an int.

class foo
{
static int bar;
};

int foo::bar = 42;

Your PartsList PartsList::GlobalPartsList; is doing the same thing, it is
just default constructed.
 
R

Rafael Anschau

Thanks, but same question.

int foo::bar = 42;//OK, =42
int foo::bar; //??

[]´s

Rafael
 
R

Rafael Anschau

Thanks but same question:

int foo::bar = 42; //Ok, =42
int foo::bar;// ?? =??

[]´s

Rafael
 
M

Martin York

Yes, my text says it initializes or defines it there. That line 159
only declares it.
But initializes it, defines it with what ? What it does is to make it
possible to later assign
a value ?

When an object is initialised it calls the objects constructor.
So line 162 creates the object and calls the objects constructor (see
constructor for PartsList). Once it has been constructed you can now
call all the public methods available in PartsList.


PartsList::GlobalPartsList.doSomthing()
 
R

Rafael Anschau

Thank you now I get it. The purpose is to make members avalible
and run the constructor. Great.

[]´s

Rafael
 
R

Rafael Anschau

Sorry, I for one don't understand. "Brackets's"?

That´s an old 80´s 90´s (70´s ?) electronic compliment.

[]´s

Rafael
 
J

James Kanze

Thank you now I get it. The purpose is to make members
avalible and run the constructor.

Or not. Even if the object doesn't have a constructor, you need
a definition. Very basically, it's the definition which
allocates the memory for the object (or causes the
compiler/linker to allocate it).
 
R

Rolf Magnus

Rafael said:
Sorry, I for one don't understand. "Brackets's"?

That´s an old 80´s 90´s (70´s ?) electronic compliment.

[]´s

But it's probably supposed to use the apostrohpe (') and not the
french "accent aigu" over an empty space (´).
 
R

Rolf Magnus

Rafael said:
Thank you now I get it. The purpose is to make members avalible
and run the constructor. Great.

No, it isn't. Think of the declaration as an entry in the table of contents
in a book. It tells you which chapters exist and how to find them, but it
doesn't contain the actual chapter text. So you need to add the chapter
content somwehere to your book, just like you need to define the static
member variable somewhere, since otherwise, the TOC entry leads to nowhere.
 
R

Rolf Magnus

Rafael said:
Sorry, I for one don't understand. "Brackets's"?

That´s an old 80´s 90´s (70´s ?) electronic compliment.

[]´s

But it's probably supposed to be using an apostrophe and not an acute accent
over an empty space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top