Class object as static member of another class 
Author Message
 Class object as static member of another class

I want to use a CArray object as a static member of a class I have defined.

Within the header (.h) file I have...

class CPlayPiece : public CObject
{
protected:
        // Lots of stuff...

public:
        // More stuff...

        // Static CArray member
        static CArray<int, int> m_CArray_colorCounter;

Quote:
}; // class CPlayPiece : public Cobject

Then in the implementation (.cpp) file I have...

// Static member initialization
CArray<int, int> CPlayPiece::m_CArray_colorCounter;
// CPlayPiece::m_CArray_colorCounter.SetSize(200);

// Default constructor
CPlayPiece::CPlayPiece(const CRect & CRect_playSurface)
{
        CPlayPiece::m_CArray_colorCounter.SetSize(200);

        // More stuff

Quote:
}

In the .cpp file I have the variable,  m_CArray_colorCounter, completely
qualified.  However, when I try to set the size of the CArray object
immediately under the line, I get a bunch of errors...

error C2143: syntax error : missing ';' before '.'
error C2501: 'm_CArray_colorCounter' : missing storage-class or type
specifiers
error C2371: 'public: static class CArray<int,int>
CPlayPiece::m_CArray_colorCounter' : redefinition; different basic types
        c:\myfiles\computer
related\vcpp_projects\general_3\tetris\playpiece.h(55) : see declaration
of 'public: static class CArray<int,int>  CPlayPiece::m_CArray_colorCounter'
error C2143: syntax error : missing ';' before '.'

However, when I include the SetSize() function in the constructor it
compiles.  My concern is that with each new instance of CPlayPiece the
array will be resized.

Why can I not set the size outside of the scope of the constructor?  Is
this the correct way of using a class object as a static member of
another class?

Thanks.

Best regards,

Thomas



Wed, 08 Dec 2004 20:56:27 GMT  
 Class object as static member of another class

Quote:

>I want to use a CArray object as a static member of a class I have defined.

>Within the header (.h) file I have...

>class CPlayPiece : public CObject
>{
>protected:
>    // Lots of stuff...

>public:
>    // More stuff...

>    // Static CArray member
>    static CArray<int, int> m_CArray_colorCounter;

>}; // class CPlayPiece : public Cobject

>Then in the implementation (.cpp) file I have...

>// Static member initialization
>CArray<int, int> CPlayPiece::m_CArray_colorCounter;
>// CPlayPiece::m_CArray_colorCounter.SetSize(200);

>// Default constructor
>CPlayPiece::CPlayPiece(const CRect & CRect_playSurface)
>{
>    CPlayPiece::m_CArray_colorCounter.SetSize(200);

>    // More stuff
>}

>In the .cpp file I have the variable,  m_CArray_colorCounter, completely
>qualified.  However, when I try to set the size of the CArray object
>immediately under the line, I get a bunch of errors...

>error C2143: syntax error : missing ';' before '.'
>error C2501: 'm_CArray_colorCounter' : missing storage-class or type
>specifiers
>error C2371: 'public: static class CArray<int,int>
>CPlayPiece::m_CArray_colorCounter' : redefinition; different basic types
>        c:\myfiles\computer
>related\vcpp_projects\general_3\tetris\playpiece.h(55) : see declaration
>of 'public: static class CArray<int,int>  CPlayPiece::m_CArray_colorCounter'
>error C2143: syntax error : missing ';' before '.'

>However, when I include the SetSize() function in the constructor it
>compiles.  My concern is that with each new instance of CPlayPiece the
>array will be resized.

>Why can I not set the size outside of the scope of the constructor?  Is
>this the correct way of using a class object as a static member of
>another class?

You can't write an arbitrary statement in namespace or global scope.
You can only write various declarations there. Your statement:

 CPlayPiece::m_CArray_colorCounter.SetSize(200);

is illegal in this context. If CArray has no ctor which allows you to
set the size, and you still want to set its size the moment it's
created, you'll have to wrap it in a class, whose ctor sets the size,
and use that class for your static member variable. Then your
definition of the static member would look something like:

 MyArray CPlayPiece::m_MyArray_colorCounter(200);

--
Doug Harrison
Microsoft MVP - Visual C++
Eluent Software, LLC
http://www.eluent.com
Tools for VC++, VS.NET, and Windows



Wed, 08 Dec 2004 21:54:35 GMT  
 Class object as static member of another class
Thanks, Doug.

Thomas

Quote:

> You can't write an arbitrary statement in namespace or global scope.
> You can only write various declarations there. Your statement:

>  CPlayPiece::m_CArray_colorCounter.SetSize(200);

> is illegal in this context. If CArray has no ctor which allows you to
> set the size, and you still want to set its size the moment it's
> created, you'll have to wrap it in a class, whose ctor sets the size,
> and use that class for your static member variable. Then your
> definition of the static member would look something like:

>  MyArray CPlayPiece::m_MyArray_colorCounter(200);



Thu, 09 Dec 2004 16:35:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. does static class member change class size?

2. An object as a static class member

3. const static class member object

4. help with global and/or static class member objects

5. help with global and/or static class member objects

6. Creating a static class from non static Framework classes

7. how to call a non-static member function of a class from a static member function of the class?

8. Help: Trouble calling object classes within other object classes

9. accessing static member of Windows service class thro reflection - need help

10. Initialising a static data member of a Class?

11. Initialising a static data member of a Class?

12. static class members - ahh!!

 

 
Powered by phpBB® Forum Software