2

In C# I can do this:

foo( new SomeClass() { SomeProperty = "value" } );

Now in C++/CLI I can equivalently do this:

auto tmp = gcnew SomeClass();
tmp->SomeProperty="value";
foo (tmp);

But is there a syntax for C++/CLI that is similar to C# object initializers? In particular I don't want to define a temporary variable and I can't add arguments to the constructor itself.

Kevin Holt
  • 775
  • 9
  • 15
  • AFAIK there is no such initialization possible in C++/CLI. New constructor would be the way to go - why can't you have it? – Marek Fekete Oct 01 '16 at 19:30
  • Well, it's not totally out of the question. But there are a decent number of properties that are all optional. So it would mean many different constructors to handle all the different combinations. C# object initializers handle this beautifully so I was just hoping for a c++/cli analog. Otherwise I think the temporary variable will probably be the less painful route for my case. – Kevin Holt Oct 01 '16 at 19:54
  • I see. Well, another option may be to have a constructor taking all possible properties in by reference, passing `nullptr` in place of uninitialized properties. I think your approach is more readable though. – Marek Fekete Oct 01 '16 at 21:11

0 Answers0