CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    How to define a static member data in an abstract class?

    For example, in a header file A.h, I define an abstract class,
    Code:
    // A.h
    class A
    {
    public:
        virtual void foo() = 0;
    private:
       static int _x;
    };
    How'd I initialize static member data _x?Normally, we initialize a static member data in a cpp file. However, there is not cpp file for A.h. If I intialize _x in header file, there will be linker errors like mulitple defined symbols. What is appropriate way to do that? Thanks.

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

    Re: How to define a static member data in an abstract class?

    Initialise it in a source file, e.g.,
    Code:
    int A::_x = 123;
    If there is no source file, then you should create one. Do you really need a static non-const member variable, anyway?
    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

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to define a static member data in an abstract class?

    Quote Originally Posted by laserlight View Post
    Initialise it in a source file, e.g.,
    Code:
    int A::_x = 123;
    If there is no source file, then you should create one. Do you really need a static non-const member variable, anyway?
    Yes, I really need a static non-const member variable because only one copy of this variable is needed. For an abstract class to define a static member data, we always need to have a source file for an abstract class? Thanks.

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

    Re: How to define a static member data in an abstract class?

    Well, you need to define the static member variable somewhere. With the notable exception of static const integral member variables, this means defining it outside of the class definition. Since doing this in the header is wrong as the header may be included in multiple translation units, you should define the variable in some source file.
    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

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to define a static member data in an abstract class?

    Quote Originally Posted by laserlight View Post
    Well, you need to define the static member variable somewhere. With the notable exception of static const integral member variables, this means defining it outside of the class definition. Since doing this in the header is wrong as the header may be included in multiple translation units, you should define the variable in some source file.
    When you said I should define the variable in some source file, you meant any source file? I tried to define the variable in A.cpp(from AA.dll), then I got linkers error from B.obj(from BB.dll). The linker errors are only caused because I define the static variable in A.cpp. Do you have any idea why it happened? Thanks.

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