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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:30:56+00:00 2026-05-26T19:30:56+00:00

How to declare a constant array in class with constant class variable? Is it

  • 0

How to declare a constant array in class with constant class variable? Is it possible.
I don’t want dynamic array.

I mean something like this:

class test
{
    const int size;
    int array[size];
    public:
    test():size(50)
    {}
}

int main()
{
    test t(500);
    return 0;
}

the above code gives errors

  • 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-26T19:30:56+00:00Added an answer on May 26, 2026 at 7:30 pm

    No, it’s not possible: As long as size is a dynamic variable, array[size] cannot possibly be implemented as a static array.

    If you like, think about it this way: sizeof(test) must be known at compile time (e.g. consider arrays of test). But sizeof(test) == sizeof(int) * (1 + size) in your hypothetical example, which isn’t a compile-time known value!

    You can make size into a template parameter; that’s about the only solution:

    template <unsigned int N>
    class Test
    {
      int array[N];
      static const unsigned int size = N; // unnecessary really
    public:
      // ...
    };
    

    Usage: Test<50> x;

    Note that now we have sizeof(Test<N>) == sizeof(int) * (1 + N), which is in fact a compile-time known value, because for each N, Test<N> is a distinct type.

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

Sidebar

Related Questions

I want to declare a huge list of constant array like: my @tlds =
I want to initialize a static collection within my C# class - something like
I am trying to declare a constant array for validating type properties held by
Is this the correct way to declare and iterate through an array in Python,
I need to declare an array of pointers to functions like so: extern void
How to declare an array in which all the values (or objects) are constant
I have an array in php like this: $myArray = array('name'=>'juank', 'age'=>26, 'config'=>array('usertype'=>'admin','etc'=>'bla bla'));
for each letter in the alphabet i have an int-array declared like this: int
If I declare a constant say #define MONTHS =12 in C, I am aware
Does anyone know of a way to declare a date constant that is compatible

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.