I am trying to create objects of Contact. But I am not able to do that with initializer list. When I remove new inside Contact class constructor, it works fine but when I add new, compiler throws an error. I want to understand why I am not able to use initializer list. Where can I use initializer list and where can I not?
#include"stdafx.h"
#include<string>
using namespace std;
struct Address
{
string street;
string city;
};
struct Contact
{
string name;
Address* address;
Contact(const Contact& other)
: name{other.name},
address( new Address(*other.address) ) {}
~Contact()
{
//delete work_address;
}
};
int main()
{
auto address = new Address{ "123 East Dr", "London" };
//Issue while creating john and jane below
//Compiler error: Cannot initialize john with initializer list
Contact john{ "John Doe", address };
Contact jane{ "Jane Doe", address };
delete address;
getchar();
return;
}