2

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;
}();
athos
  • 6,120
  • 5
  • 51
  • 95
  • 1
    You only have 5 members to initialize, why not make it a part of the constructor? So you could call it like `trf_schedule = { site, deal_id, payment_date, ccy1, ccy2 };` – Fireboyd78 Jun 02 '17 at 03:03
  • trying to avoid updating constructors as the member list grow. @Fireboyd78 – athos Jun 02 '17 at 03:03
  • Ah, I see. Well unfortunately there's not really a way to do it the way you want to in C++, and your best bet would be to use a constructor overload. – Fireboyd78 Jun 02 '17 at 03:05
  • 3
    Do your design first, that will reduce "as the member list grows" effort – FKEinternet Jun 02 '17 at 03:05
  • "trying to avoid updating constructors as the member list grow." - what about destructors? – iehrlich Jun 02 '17 at 03:05
  • 1
    You may be able to take advantage of Aggregate Initialization. [What is Aggregate Initialization?](https://stackoverflow.com/questions/17712872/what-is-aggregate-initialization) Click the link and find out if Aggregate Initialization is right for you! – user4581301 Jun 02 '17 at 03:07
  • @FKEinternet i live in a world keeps growing. – athos Jun 02 '17 at 03:28
  • @iehrlich destructor is not an issue as such are POD classes,or rather, structs. – athos Jun 02 '17 at 03:28
  • @user4581301 it works for struct but without "calling the names of the class/struct members', so the 1-1 relationship is only ensured by the sequence, this is unsafe. – athos Jun 02 '17 at 03:28
  • Yup, but it's that or a constructor, I'm afraid. – user4581301 Jun 02 '17 at 03:35

2 Answers2

2

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)

dlasalle
  • 1,615
  • 3
  • 19
  • 25
1
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

  1. The corresponding members are public (which sounds like is true in your case) and no other explicit or user defined constructors are provided, or
  2. There is a constructor that takes those arguments.
R Sahu
  • 204,454
  • 14
  • 159
  • 270