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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:08:38+00:00 2026-06-13T22:08:38+00:00

I’m trying to learn more about templates and have come across a problem I

  • 0

I’m trying to learn more about templates and have come across a problem I can’t seem to solve. At the moment the class below works fine.

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

template <class T, int s>
class myArray{
public:
    T* data;
    inline T& operator[](const int i){return data[i];}
    myArray(){
        data=new T[s];
    }
    myArray(const myArray& other){
        data=new T[s];
        copy(other.data,other.data+s,data);
    }
    myArray& operator=(const myArray& other){
        data=new T[s];
        copy(other.data,other.data+s,data);
        return *this;
    }
    ~myArray(){delete [] data;}
};  

If I use it:

myArray<myArray<myArray<int,10>,20>,30> a;

a is now 30x20x10 array that I can access with the normal array brackets e.g. a[5][5][5]. I wish to add a feature so that I could write:

myArray<myArray<myArray<int,10>,20>,30> a(10);

and initialise all of the entries to 10 for example. I can’t work out how to do this. As I understand, each layer of myArray is constructed using the default constructor. If I changed this to something like:

myArray(int n=0){
        data=new T[s]; 
        fill(data,data+s,n); //T might not be of type int so this could fail.
}

I think this should fail when data is not of type int (i.e. on any array on dimensions > 1), however it doesn’t. It works when the array is square, but if not then some of the entries aren’t set to 10. Does anyone have an idea how the standard vectors class achieves this? Any help would be amazing. Thanks!

  • 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-13T22:08:39+00:00Added an answer on June 13, 2026 at 10:08 pm

    std::vector uses placement new on memory blocks. It constructs the data.after allocating the memory in a second line of code.

    This technique would work for you as well. Be careful with placement new as it requires you to call destructors manually as well.

    Here is a half-assed route without placement new:

    template<typename U>
    explicit MyArray( U const& constructFromAnythingElse )
    {
      AllocateSpace(N); // write this, just allocates space
      for (int i = 0; i < N; ++i)
      {
        Element(i) = T( constructFromAnythingElse );
      }
    }
    

    with placement new, you have to allocate the memory first, then construct in-place, and then remember to destroy each element at the end.

    The above is half-assed compared to a placement new route, because we first construct each element, then build another one, and use operator= to overwrite it.

    By making it a template constructor on an arbitrary type, we don’t rely on multiple conversion to get multiple levels down into the array. The naive version (where you take a T const&) doesn’t work because to construct an array of arrays of arrays of T, the outermost one expects an array of arrays of T as an argument, which expects an array of T as an argument, which expects a T — there are too many levels of user defined construction going on there.

    With the above template constructor, the array of array of array of T accepts any type as a constructor. As does the array of array of T, as does the array of T. Finally, the T is passed in whatever you constructed the outermost array of array of array of T, and if it doesn’t like it, you get a compiler error message that is nearly completely unreadable.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am trying to loop through a bunch of documents I have to put
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.