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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:35:35+00:00 2026-06-10T03:35:35+00:00

In C# I can generate a static array using a function: private static readonly

  • 0

In C# I can generate a static array using a function:

private static readonly ushort[] circleIndices = GenerateCircleIndices();

....

    private static ushort[] GenerateCircleIndices()
    {
        ushort[] indices = new ushort[MAXRINGSEGMENTS * 2];
        int j = 0;

        for (ushort i = 0; i < MAXRINGSEGMENTS; ++i)
        {
            indices[j++] = i;
            indices[j++] = (ushort)(i + 1);
        }

        return indices;
    }

Using C++ I believe the following is the correct way to generate a static array:

.h

static const int BOXINDICES[24];

.cpp (constructor)

static const int BOXINDICES[24] =
{ 
    0, 1, 1, 2, 
    2, 3, 3, 0, 
    4, 5, 5, 6, 
    6, 7, 7, 4, 
    0, 4, 1, 5, 
    2, 6, 3, 7 
};

How can I do the same for the circleIndices but use a function to generate the values?

.h

static const int CIRCLEINDICES[];

.cpp (constructor)

static const int CIRCLEINDICES[] = GenerateCircleIndices();  // This will not work

Do I have to initialise the array elements with values of 0 then call the function?

  • 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-10T03:35:37+00:00Added an answer on June 10, 2026 at 3:35 am

    It depends if you want the function evaluated at runtime or compile time. In C# it is going to happen at runtime, but C++ gives you the option of doing it at compile time.

    If you want to do it at runtime you can use a static initialized class:

    struct S
    {
        int X[N];
    
        S() { for (int i = 0; i < N; i++) X[i] = ...; }
    };
    
    S g_s;
    

    Here the constructor is called before the entry to main to setup your array.

    If you want to do it at compile time than you can use either templates or macros in various ways not possible in C#. If you do it at compiletime the data of the array will be calculated by the compiler and filled out with a memcpy by the loader from the static area of your application image directly into your process address space. Here is an example:

    #include <iostream>
    #include <array>
    using namespace std;
    
    constexpr int N = 10;
    constexpr int f(int x) { return x % 2 ? (x/2)+1 : (x/2); }
    
    typedef array<int, N> A;
    
    template<int... i> constexpr A fs() { return A{{ f(i)... }}; }
    
    template<int...> struct S;
    
    template<int... i> struct S<0,i...>
    { static constexpr A gs() { return fs<0,i...>(); } };
    
    template<int i, int... j> struct S<i,j...>
    { static constexpr A gs() { return S<i-1,i,j...>::gs(); } };
    
    constexpr auto X = S<N-1>::gs();
    
    int main()
    {
            cout << X[3] << endl;
    }
    

    See: C++11: Compile Time Calculation of Array

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

Sidebar

Related Questions

ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink() , @Html.BeginForm()
I know you can generate all permutations from a list, using glob or Algorithm::Permute
Does anyone know a C# framework that can generate public/private keys, X.509 certificates and
Using System, how can I dump the contents of a multidimensional array to the
static void LasVegas(int []tablero, int f, int ultimaReina){ HashSet<Integer> enterosUsados = new HashSet<Integer>(); if
var checkduplicates = new Array(); drawOne(i); //console.log(checkduplicates) function drawOne(i) { //randomly select one photo
I can generate my models and schema.yml file based on an existing database. But
ASP.NET MVC 3 can generate scaffold code for us(controllers and views). The generated views
I found jquery can generate file tree structure based on existing folders and files
I'm wondering if i can generate a scaffold without getting that annyoing s after

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.