In C# I could do this:
char[] a = new char[] {'a', 'a', 'a'};
But can I do something like that in C++? I tried:
char *a = new char [] {'a', 'a', 'a'};
But it doesn't compile.
This is a bug in the C++ spec (which doesn't let this simple construct to compile). You need to supply the size
char *a = new char [3] {'a', 'a', 'a'};
See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1469 . Note that if you parenthesize the type name it is a type-id and not a new-type-id and hence syntactically allows you to omit the size expression. So you may be able to find an implementation that allows you to say
char *a = new (char[]){'a', 'a', 'a'};
Althought it is clear that it wasn't the explicit intent that this is possible (and some rules in the new
paragraphs can be interpreted to forbid it).
why not just do this? :
char a[] = {'a', 'a', 'a'};
Also avoid using arrays completely. Use std::vector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With