I want to add an object to an array. I'm trying to do it in the constructor within this. I'm starting to learn c++ and I'm not sure why it doesn't work in this way. Can someone explain why it won't work and how to do it properly?
#include <iostream>
using namespace std;
const int CAPACITY = 50;
class Photo
{
private:
string title, description, size;
int year, month;
static Photo collection[CAPACITY];
static int collection_size;
public:
Photo(string title, string description, string size, int year, int month)
:title(title), description(description), size(size), year(year), month(month)
{
collection[collection_size++] = this;
}
Photo()
:Photo("", "", "1920x1080", 2021, 1)
{
}
static int get_collection_size(){
return collection_size+1;
}
static void print_collection(){
for(auto photo : collection){
cout << photo.size;
}
}
};
Photo Photo::collection[CAPACITY] = {};
int Photo::collection_size = 0;
int main(){
Photo p;
p.print_collection();
return 0;
}
&)