Thread: Static Data Member

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Static Data Member

    referring to this - http://publib.boulder.ibm.com/infoce...ef/cplr038.htm

    "You must define the static member outside of the class declaration, in namespace scope." - namespace scope?

    I tried to define a static member declared in a class in main() , and it gave me error, i also read somewhere that static members should always be defined in global scope.how does this all relate?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    class foo
    {
    public:
        static int bar;
    };
    
    int foo::bar;
    
    int main()
    {
        ...
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    class foo
    {
    public:
        static int bar;
    };
    
    int foo::bar;
    
    int main()
    {
        ...
    }
    what if i place static int bar in private ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what if i place static int bar in private ?
    Exact same thing.

    >> namespace scope?
    Do you know what a namespace is? If not, you have to understand them first. If yes, then you know that you can declare your class inside a namespace. The quote is saying that the static member must be defined inside the scope as the class. If you define it inside the main() function it won't work, that is a deifferent scope than the class is in.

    If you aren't using namespaces, then the proper scope to define the static member is the global scope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  3. compilation problems with static data member
    By viral612 in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2008, 06:55 AM
  4. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 08:46 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM