5
class Try {
    const int no = 5;
    int arr[no];
};

Here is a simple class , but I get this compilation error. no is constant so I thought it should not be the problem.

0

1 Answer 1

11

arr must have the same size in all instances of your class. no is const but that only means it never changes after an instance is created. It doesn't mean that it is the same for all instances all the time. For example, no can be set in the initializer list of the constructor

Foo::Foo(int size) : no(size)
{}

For this reason, unless you make no static, you can't use it as array size because that would imply potentially differently sized arrays in each instance.

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

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.