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

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    9

    Post Need some clarification with a static class with static data members?

    Hello.

    To start off I have a header file with a class that holds static data and static data members. Something like this:

    class ImAClass
    {
    private:
    static std::vector<A*> listOfA_;
    public:
    ImAClass(void); //non-default constructor
    static std::vector<A*>* GetListOfA(void) { return &listOfA_; }
    };


    And another class that holds a static version of this class:

    class IHoldThings
    {
    public:
    static ImAClass noReallyImAClass_;
    };


    Why is it that I get no errors when I define this outside of both classes?:

    std::vector<A*> ImAClass::listOfA_; //seemingly does nothing? maybe??


    But I get an unresolved external symbol error when I omit it?

    Does listOfA_ exist before the compiler sees this call? If that is true, should I also construct the static class in IHoldThings in the same fashion, or is ImAClass's constructor enough for this purpose? Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Need some clarification with a static class with static data members?

    Quote Originally Posted by Akira__tyler
    Why is it that I get no errors when I define this outside of both classes?:

    std::vector<A*> ImAClass::listOfA_; //seemingly does nothing? maybe??
    Because you are correctly defining the static member variable (assuming that you do so in exactly one source file(.

    Quote Originally Posted by Akira__tyler
    But I get an unresolved external symbol error when I omit it?
    Because without it, you only declared the static member variable, but never defined it.

    By the way, contrary to the comment, this line declares a default constructor:
    Code:
    ImAClass(void); //non-default constructor
    Remember to post your code in [code][/code] bbcode tags.
    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

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured