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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:39:28+00:00 2026-05-29T05:39:28+00:00

The sample code below shows what I am trying to do: template<int NX, int

  • 0

The sample code below shows what I am trying to do:

template<int NX, int NY>
class Array
{
public:
  float v[NX][NY];
};

void main()
{
  Array<10,20> grid;
}

The above code won’t compile, but it shows what I want to do. We have a class that contains an array, and the array class doesn’t know its size until compile time. Is there a simple way to do this?

Edit: I want to write a simple reusable array class. That means I need to find a good way to separate the array size from the class.

I also want the class to be fast (and simple) so it must not be dynamically allocated. That means the size can’t be given during run time.

I also don’t want to use the preprocesser to define the size because that means I will have to go through the hassle of changing a number somewhere. That isn’t convenient enough.

Basically, the class doesn’t know its own size until compile time, because that is when the main function tells the class its size.

Edit: The above code is good.

  • 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-29T05:39:29+00:00Added an answer on May 29, 2026 at 5:39 am

    Other than main not returning an int, this is legal code and should compile. In fact, on some compilers this will compile without main returning an int, such as VC++ but this is non-standard behaviour.

    You can also store the size at compile time so that you don’t have to calculate it manually.

    #include <iostream>
    
    template<int NX, int NY>
    class Array
    {
    public:
      float v[NX][NY];
    
      int size() const { return ArraySize; }
    
    private:
      enum { ArraySize = NX * NY }; // You can also store rows/cols individually
    };
    
    int main()
    {
      Array<10,20> grid;
    
      std::cout << grid.size();
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below shows a sample that I've used recently to explain the different
I have a plot (sample code pasted below) that I am trying to add
The code below generates a warning CS3006 Overloaded method MyNamespace.Sample.MyMethod(int[])' differing only in ref
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
In the sample code below I am dividing by zero which when I step
In the below code sample, what does {0:X2} mean? This is from the reflection
I've put together a small code-sample below (Currently in C# 3.5 but would also
Below is sample code to create a radio button element with Yes/No options in
I'm trying to pass a parameter in the onclick event. Below is a sample
We have code similar to the one below. We are trying to achieve RAII

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.