dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C++ static class members,

  1. #1
    Join Date
    Jun 2009
    Posts
    46

    Question C++ static class members,

    Greetings all,

    I read that static members should be declared outside class definition:
    http://weblogs.asp.net/whaggard/arch...05/252685.aspx

    1)but i noticed that static integer data members can be declared in the header file.Is this only for integers ?

    2) If I want to declare constants inside classes, what should i use ? extern or static?

    //In Class.h
    Code:
    extern const std:string CONST;
    or
    Code:
    static const std:string CONST;
    ?

    but either case I have to declare something like follows in cpp:
    Code:
    //Class.cpp
    const std:string CONST="Some Value";

    thanks
    umanga

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C++ static class members,

    Quote Originally Posted by umanga View Post
    2) If I want to declare constants inside classes, what should i use ? extern or static?
    const or static const (depends on their type and purpose)


    Code:
    // H
    #include <string>
    
    struct Test
    {
        Test::Test(const std::string &name)
            : INSTANCE_NAME(name)
        {
        }
    
        static const int MAX_ID = 10;
    
        static const std::string STRUCT_NAME;
    
        const std::string INSTANCE_NAME;
    
    private:
        // Disabled.
        Test &operator = (const Test &rhs);
    };
    
    // CPP
    #include <iostream>
    
    #include "Test.h"
    using namespace std;
    
    const string Test::STRUCT_NAME("Test");
    
    int main()
    {
        Test object("object");
    
        cout << "Test::STRUCT_NAME = " << Test::STRUCT_NAME << "\n";
        cout << "object.INSTANCE_NAME = " << object.INSTANCE_NAME << "\n";
        cout << "Test::MAX_ID = " << Test::MAX_ID << "\n";
    };
    Last edited by JohnW@Wessex; August 24th, 2009 at 09:50 AM.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: C++ static class members,

    1)but i noticed that static integer data members can be declared in the header file.Is this only for integers ?

    Static Const is legal for integral type only and not float or double.


    static const std::string STRUCT_NAME;
    const string Test::STRUCT_NAME("Test");

    I don't know that static const here is legal.
    Thanks for your help.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,761

    Re: C++ static class members,

    Quote Originally Posted by Peter_APIIT
    Static Const is legal for integral type only and not float or double.
    That is not true. The initialisation of a static const object in the class definition is legal only for integral types, but that does not mean one cannot declare a static const object of a non-integral type in the class definition and define it in a source file, as JohnW@Wessex demonstrated.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)