initializing private static members

M

Madhav

hi all,
I was trying the following code on the MS .net compiler:

class Item {
static int count;

public:
//static void init() { }

Item()
{
count++;
cout<<"constructing Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroying Item #"<<count<<endl;
}
};

The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.

Thanks,
Madhav.
 
I

Ian Collins

Madhav said:
hi all,
I was trying the following code on the MS .net compiler:

class Item {
static int count;

public:
//static void init() { }

Item()
{
count++;
cout<<"constructing Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroying Item #"<<count<<endl;
}
};

The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.
Do you have a definition of Item::count in a source file? Sounds like
you have forgotten to do this.
 
L

luckyboy

try to make initialization in this manner in your constructor:
Item() :count(0)
{
count++;
cout<<"constructing Item"<<endl;
}

this manner is called the initializer list.
Note that you can't initialize constant members in a class by =
operator, instead you should use initializer list.
if there are many variables that you want to initialize feel free to
use comma, i.e.
Item():count1(o), count2(3), count3(9)
{
count1++;
count2++;
count3++;
}
 
I

Ian Collins

luckyboy said:
try to make initialization in this manner in your constructor:
Item() :count(0)

No, this is wrong.

If you look back to the code you snipped, count is a static member. It
requires a definition.
 
R

recover

Yes, you must write " int Item::count=0; " in the source file (normal in
the .cpp file).
 
L

luckyboy

Ian said:
No, this is wrong.

If you look back to the code you snipped, count is a static member. It
requires a definition.

Thanx Ian Collins., this really helped me, but i wanna mention that i
still beginner in C++ field and just i wanna help others to solve their
problems. Again Thank you :)
 
M

Madhav

Ian said:
No, this is wrong.

If you look back to the code you snipped, count is a static member. It
requires a definition.

hey Ian,
I wrote this in the main function by making the count
variable public:

int Item::count=0;

But now the compiler gives me something like:

error C2655: 'Item::count' : definition or redeclaration illegal in
current scope
see declaration of 'Item::count'

Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?

I am not sure if the static variables follow the rules of
global variables for initialization. But I think this may just be it.

Regards,
Madhav.
 
I

Ian Collins

Madhav wrote:

Please try and keep some context, you should have replied to my first
posting.
hey Ian,
I wrote this in the main function by making the count
variable public:

int Item::count=0;
There's your mistake, the definition should be in the global scope (or
the same namespace as the class), not in main.
But now the compiler gives me something like:

error C2655: 'Item::count' : definition or redeclaration illegal in
current scope
see declaration of 'Item::count'
It's telling you you can't put the definition in main.
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
I think I just have!
 
M

Madhav

Ian said:
I think I just have!
hm...but since count is a private variable, why I don't get error for
trying to access a private class member outside of the class? or this
case is an exception?
 
M

mlimber

Madhav said:
hm...but since count is a private variable, why I don't get error for
trying to access a private class member outside of the class? or this
case is an exception?

It's still private, but with static members, you must *declare* them in
the class but *define* them outside. Similarly, you might have a
private member function that is declared in the class but defined
outside of it:

class A
{
void DoSomething();
// ...
};

void A::DoSomething() { /*...*/ }

Cheers! --M
 
M

Madhav

mlimber said:
It's still private, but with static members, you must *declare* them in
the class but *define* them outside. Similarly, you might have a
private member function that is declared in the class but defined
outside of it:

class A
{
void DoSomething();
// ...
};

void A::DoSomething() { /*...*/ }

Cheers! --M

Yes...hmm...thanks a lot Mr. X!
 
M

Marcus Kwok

recover said:
Thanks. I will be careful of.
I use Outlook Express to reply news. It's template take me to wite at begin
of letter.

Google for [outlook express quote fix].
 
H

Howard

Madhav said:
hm...but since count is a private variable, why I don't get error for
trying to access a private class member outside of the class? or this
case is an exception?

Because you're _not_ trying to access it outside the class. You're writing
a definition, which is different from an assignment statement.

An assignment, if it had worked for this, _would_ have gone in main or some
other function. A definition of a static member has to go _outside_ of any
function. I know it looks like an assignment, but it's not. It's an
initialization of a static member, which is called a "definition" of that
member.

-Howard
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top