Doruk
Doruk

Reputation: 3

Constructor for Classes with Array Field in C++

I have a class called "File" with private field "bool[] bits". How should I create a constructor for this "File" class which only includes "bool[] bits"? Size of array is unknown. Thanks

Upvotes: 0

Views: 183

Answers (1)

masoud
masoud

Reputation: 56479

Size of built-in arrays in C++ is part of its type. So, you can not create an array without specifying its size. BTW, Your code bool[] bits is not a valid syntax in C++.

class File
{
    std::vector<bool> bits;
public:
    File(int size) : bits(size) {}
};

Upvotes: 1

Related Questions