3

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

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

13 Comments

Isn't this just a template version of the class specific static integer constant approach?
@Als: well, yes, in as far as the "class-specific static integer" can now be specified by the author via the template parameter.
@Als: If you mean, this and your answer is almost same, then no. You can have Test<10>, Test<20>, Test<30> and so on, but in your case, the size is fixed: you can have only array[size] and size is fixed at 50.
@Nawaz: Please spare me the interference of flaring this in to something which it is not.This is not about who's answer is better as you are trying to make it in to.
@Als: What part of my comment do you disagree with?
|
3

You mean a fixed sized array? You could use std::array like this:

#include <array>

class test
{
    static const size_t s_size = 50;
    std::array<int, s_size>   m_array;
public:
    test()
    {
    }
};

Or if you want to support different sizes you need to resort to a class template like this:

#include <array>

template <size_t SIZE>
class test
{
    std::array<int, SIZE>   m_array;
public:
    test()
    {
    }
};

std:array has the added benefit of keeping the size information along with the member (unlike arrays which decay to pointers) and is compatible with the standard library algorithms.

There is also a version that Boost offers (boost::array) which is similar.

2 Comments

I want to use a constant class variable instead of 50. Is there any way to do this?
Only if it is a compile time constant. Updated code in the answer.
1

Your code yields an error because compiler needs to know the size of data type of each member. When you write int arr[N] type of member arr is "an array of N integers" where N must be known number in compile time.

One solution is using enum:

class test
{
    enum 
    {
        size = 50
    };

    int arr[size];
   public:
    test() {}
};

Another is declaring size as static const member of class:

class test
{
    static const int size = 50; 
    int arr[size];
   public:
    test(){}
};

Note that in-class initialization is allowed only for static class integers! For other types you need to initialize them in code file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.