Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:13:53+00:00 2026-06-13T06:13:53+00:00

I have template class Array where the template type T should be either pointer

  • 0

I have template class Array where the template type T should be either pointer or not-pointer type.

template<class T>
class TArray
{   
     static const int max_len=100;
     T* data;
     int length;

  public:
     TArray(){data=new T[max_len]; this->length=0;} 
     void Add(T value){data[length++]=value;}   
     ~TArray();
};

The problem is how to free space, since we can not call delete for not pointer types like this

template<class T>
TArray<T>::~TArray()
{
    //for(int i =0; i < length; i++)
    //  delete data[i]; // NOT WORKING WITH OBJECTS THAT ARE NOT POINTERS !!!
    delete[] data;
}

Let’s have addition class

class A
{
    int* a;
  public:
    A(int n){a = new int[n];}
    ~A(){delete[] a;}
};

and make two instances of template class

// Create array of doubles
TArray<double>* double_array = new TArray<double>();
delete double_array;

// Create array of pointers to class A
TArray<A*>* A_array = new TArray<A*>();
A* a = new A(5);
A_array->Add(a);
delete A_array;

When I call destructor for TArray<A*> I need to call destructor for class A but I don’t know how, since the commented code in destructor is not compile (C2541) if we make for example array of doubles.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:13:54+00:00Added an answer on June 13, 2026 at 6:13 am

    You can develop two versions for your template. First, write the normal version template<class T>. Then, write a second pointer-only version that specializes your template by declaring it like this:

    template<class T>
    class TArray<T*>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class ArrayTemp that creates an array of pointers to type
I have the following structure: template <class T> struct Array{ int lenght; T *
Possible Duplicate: Convert std::tuple to std::array C++11 Suppose you have: template<class T,int N> struct
I have the template class and array of pointers to objects and overloaded logic
I have the following class: #include <array> template<unsigned short D> class Point { private:
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I defined a class with a template that defines a generic array type T
I have a template class, called OneCell , here the definition: template <class T>
I have a template class, called Cell , here the definition: template <class T>
I have a template class: template<class T> class A{ T a, b; public: A(A<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.