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

    help in dynamic array declaration

    template <class AType,int size> class atype
    {
    // AType a[size];
    AType *a=new AType[size];



    When i am declaring an array in the above way an error is being shown.
    plz help me to solve it.

    the error is as follows

    cant initialize a class memeber here.
    I m new in dis areana.

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: help in dynamic array declaration

    You have to allocate memory in the constructor and then releasing in the destructor.

    Code:
    template <class AType,int size> class atype
    {
        AType *a;
    
    public:
        atype()
       {
            a = new AType[size];  // Allocating memory.
       }
    
        virtual ~atype()
        {
            delete []a;  // Don't forget to release memory when not needed.
        }
    };

  3. #3
    Join Date
    Jul 2005
    Posts
    20

    Re: help in dynamic array declaration

    thank u.its working fine.

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