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 6894441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:46:59+00:00 2026-05-27T06:46:59+00:00

I want a template class to contain two template dynamic arrays as member variables.

  • 0

I want a template class to contain two template dynamic arrays as member variables. I use also a member function to assign to them values. However I got problem with the code and I do not know the reason (probably the syntax). What is the problem in the following code?

template<typename T>
class ArrayHolder
 {
public:
ArrayHolder();
void setArrays( T [], T [],int,int);


private:
T *array1;
T *array2;
};


template<typename T>
ArrayHolderHolder<T>::setArrays(T firstarray[],T secondarray[] ,int N1, int N2)
{
array1 = new T[N1];
array2 = new T[N2];


}

After the initialization of the dynamic arrays in setArrays, what is the most effective to copy the arrays from the parameters (firstarray,secondarray) to them?

  • 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-05-27T06:47:00+00:00Added an answer on May 27, 2026 at 6:47 am
    ArrayHolderHolder<T>::setArrays(T firstarray[],T secondarray[] ,int N1, int N2)
    ^^^^^^^^^^^^^^^^^
    

    Here is one typo. The class name is ArrayHolder, notArrayHolderHolder. Also, you didn’t write the return type.

    Since you cannot pass array (by value), it is better if you use pointer notation function in parameter list, and since it is class template, prefer defining the functions in the class itself:

    template<typename T>
    class ArrayHolder
    {
      public:
         ArrayHolder();
    
         void setArrays(T *firstarray,T *secondarray ,int N1, int N2)
         {
            array1 = new T[N1];
            array2 = new T[N2];
    
            //if you want to copy, then use `std::copy` as:
            std::copy(firstarray, firstarray + N1, array1);
            std::copy(secondarray, secondarray + N2, array2);
         }
    
    private:
       T *array1;
       T *array2;
    };
    

    By the way, instead of raw arrays, you could use st::vector as:

    std::vector<T> array1;
    std::vector<T> array2;
    

    And same in the parameter list of setArrays as well. If you do so, then setArray would become this:

    void setArrays(const std::vector<T> & first, const std::vector<T> & second)
    {
        //maybe you need to do this!
        array1.clear();
        array2.clear();
    
        array1.insert(array1.end(), first.begin(), first.end());
        array2.insert(array2.end(), second.begin(), second.end());
    }
    

    Simple, isn’t it? No memory allocation, no deallocation!

    Or even better, if you accept the arguments by value, then you could write this:

    void setArrays(std::vector<T> first, std::vector<T> second)
    {
        //no need to clear!
        array1.swap(first);
        array2.swap(second);
    }
    

    The last implementation is an idiomatic solution, preferred by library implementers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to align my member variables based on a class template type but
I have a template class where I want to use objects of that class
i want a specialize template in a pointer-to-member-function case. Is there a way to
I want to have a template class that looks something like what I have
I want to create a template class in C#, for example: public class Foo<T>
I want to use this pure HTML/CSS template for my ASP.NET website: http://sub3.tanguay.de I
I have a class with a template parameter which should decide which of two
I have a template class, inside which i have a normal function. But i
If a template class definition contains a static member variable that depends on the
I'm generating a class from an interface using T4 templates, and I want to

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.