class book{
private:
int numOfPages;
public:
book(int i){
numOfPages = i;
};
};
class library{
private:
book * arrOfBooks;
public:
library(int x, int y){
arrOfBooks = new book[x](y);
};
};
int main()
{
library(2, 4);
};
With the example code above I would like to create a library of books that all have the same number of pages. So in the constructor of the library object, whenever a new book is created to be placed in the array I pass the argument in the parenthesis. The above code when tested in C++ shell shows error: "parenthesized initializer in array new". This is for the completion of a school project and no vectors are allowed (as it would be wise to do as I found doing my research) though I cannot think of any other ways to do it than the one shown above...
There is no syntax for initializing elements of a dynamic array using a non-default constructor.
You have to create the array first, then loop over the elements and assign each individually. Possibly the simplest way to do that is to use std::fill
.