By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

why static member is required to redeclare outside the class ?

P: 2
why static member is required to redeclare outside the class ?
Nov 20 '10 #1
Share this Question
Share on Google+
2 Replies


P: 2
why static member is required to redeclare outside the class ?
Nov 20 '10 #2

Banfa
Expert Mod 5K+
P: 8,916
I assume you are asking about static data members (as opposed to static functions such as

Expand|Select|Wrap|Line Numbers
  1. class Example
  2. {
  3. public:
  4.   static int m_Data;
  5. };
  6.  
  7. int Example::m_Data = 0;
  8.  
The answer is that it isn't re-declared. Line 4 declares the static data member m_Data and line 7 defines it.

A declaration just tells the compiler it exists where as a definition causes the compiler to actually allocate the allocate the memory to contain the data member.

If line 4 caused the compiler to do the definition then in a multifile project the data member would be defined in every cpp file that included the header containing the class definition and the linker would through up symbol multiply defined errors.
Nov 21 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.