[haiku-development] Re: c++ struct assignment like in c?

  • From: Clemens <clemens.zeidler@xxxxxxxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Mon, 28 Mar 2011 09:39:10 +1300

On Mon, 28 Mar 2011 05:45:38 +1300, Alex Wilson <yourpalal2@xxxxxxxxx> wrote:



You should be able to do something like this:

ps2_dev ps2_device[PS2_DEVICE_COUNT] = {
    ps2DevBuilder("input/mouse/ps2/0")
        .active(false)
        .idx(0)
        .result_sem(-1)
       .command(standard_command_timeout)
    ,
    ps2DevBuilder()
     //... etc.
}

Just have the ps2DevBuilder methods return a reference to 'this', and
provide an 'operator ps2_dev()' method. This also allows you to have
defaults for all your values. This is called the 'Named Parameter Idiom' and
you can read about it here:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20 Hope that helps
:)

Yes in theory this is nice but I think it is a bit overkill for this use-case(?). c++ should have some this pattern integrated!

On Mon, 28 Mar 2011 09:26:53 +1300, Axel Dörfler <axeld@xxxxxxxxxxxxxxxx> wrote:

This is, unfortunately, not valid in C++, you either have to resort to
something like Alex suggested, or you could just remove the ".name = "
stuff, and keep a standard initializer list - less verbose, but it'll
work just fine.

Tried this first but the struct is quit large and it would be a long, unreadable, error prone list. Think I will assign all the values after creating the array...

thanks anyway!
        Clemens

Other related posts: