Reputation: 6405
I have a class DealSchedule with some members defined, now I have to initialize it with something like:
DealSchedule trf_schedule;
trf_schedule.site_ = site;
trf_schedule.deal_id_ = deal_id;
trf_schedule.payment_date_ = payment_date;
trf_schedule.ccy1_ = ccy1;
trf_schedule.ccy2_ = ccy2;
Is it possible to write nicely as
DealSchedule trf_schedule
{
site_ = site;
deal_id_ = deal_id;
payment_date_ = payment_date;
ccy1_ = ccy1;
ccy2_ = ccy2;
};
this would protect changes to the initialization.
I can come out with something like this (need c++14 supporting renaming in lambda capturing):
DealSchedule trf_schedule;
[&_=trf_schedule]()
{
_.site_ = site;
_.deal_id_ = deal_id;
_.payment_date_ = payment_date;
_.ccy1_ = ccy1;
_.ccy2_ = ccy2;
}();
Upvotes: 2
Views: 88
Reputation: 1725
The correct answer is R_Sahu's, but in the event you really want that syntax and are feeling self-destructive...
struct _ : public DealSchedule { _() {
site_ = site;
deal_id_ = deal_id;
payment_date_ = payment_date;
ccy1_ = ccy1;
ccy2_ = ccy2;
}} trf_schedule;
This uses an inline struct for initialization, which you can then slice back to a DealSchedule
.
(Don't actually do this)
Upvotes: 2
Reputation: 206577
DealSchedule trf_schedule
{
site_ = site;
deal_id_ = deal_id;
payment_date_ = payment_date;
ccy1_ = ccy1;
ccy2_ = ccy2;
};
is not valid C++ syntax to initialize an object. You can initialize the object using
DealSchedule trf_schedule = {site, deal_id, payment_date, ccy1, ccy2};
if
public
(which sounds like is true in your case) and no other explicit or user defined constructors are provided, orUpvotes: 1