Skip to main content
Add destructor for solution 2 and a note about the Rules of Three/Five.
Source Link
the busybee
  • 2.4k
  • 10
  • 19

At the time the constructor of your class is called, its size must be already defined, because the memory allocation takes place before. But you try to give it as a parameter to the constructor. This cannot work.

You have at least these alternatives. (Note: This is an invitation to edit this answer and add more, if you know some serious solution.)

1. Use a template for the size

Please be aware that each different size will generate another implementation in machine code.

template<size_t size>
class myLib {
public:
    inline myLib<size>() {}
private:
    bool data[size];
};

2. Allocate the requested memory dynamically

class myLib {
public:
    inline explicit myLib(size_t size) : data(new bool[size]) {}
    inline ~myLib() {
        delete[] data;
    }
private:
    bool* data;
};

Note: Depending on the version of C++ you're using, you need to consider the Rule of Three (before C++11) or the Rule of Five (beginning with C++11). Since the implementation of the missing methods depends on specific details, it is left as an exercise to the reader.

At the time the constructor of your class is called, its size must be already defined, because the memory allocation takes place before. But you try to give it as a parameter to the constructor. This cannot work.

You have at least these alternatives. (Note: This is an invitation to edit this answer and add more, if you know some serious solution.)

1. Use a template for the size

Please be aware that each different size will generate another implementation in machine code.

template<size_t size>
class myLib {
public:
    inline myLib<size>() {}
private:
    bool data[size];
};

2. Allocate the requested memory dynamically

class myLib {
public:
    inline explicit myLib(size_t size) : data(new bool[size]) {}
private:
    bool* data;
};

At the time the constructor of your class is called, its size must be already defined, because the memory allocation takes place before. But you try to give it as a parameter to the constructor. This cannot work.

You have at least these alternatives. (Note: This is an invitation to edit this answer and add more, if you know some serious solution.)

1. Use a template for the size

Please be aware that each different size will generate another implementation in machine code.

template<size_t size>
class myLib {
public:
    inline myLib<size>() {}
private:
    bool data[size];
};

2. Allocate the requested memory dynamically

class myLib {
public:
    inline explicit myLib(size_t size) : data(new bool[size]) {}
    inline ~myLib() {
        delete[] data;
    }
private:
    bool* data;
};

Note: Depending on the version of C++ you're using, you need to consider the Rule of Three (before C++11) or the Rule of Five (beginning with C++11). Since the implementation of the missing methods depends on specific details, it is left as an exercise to the reader.

Source Link
the busybee
  • 2.4k
  • 10
  • 19

At the time the constructor of your class is called, its size must be already defined, because the memory allocation takes place before. But you try to give it as a parameter to the constructor. This cannot work.

You have at least these alternatives. (Note: This is an invitation to edit this answer and add more, if you know some serious solution.)

1. Use a template for the size

Please be aware that each different size will generate another implementation in machine code.

template<size_t size>
class myLib {
public:
    inline myLib<size>() {}
private:
    bool data[size];
};

2. Allocate the requested memory dynamically

class myLib {
public:
    inline explicit myLib(size_t size) : data(new bool[size]) {}
private:
    bool* data;
};