digitalmars.D.learn - Create a general array
- Robert Rimoczi (4/4) Jul 24 2014 Hi!
- Brad Anderson (17/21) Jul 24 2014 void main()
Hi! Can I somehow make a general array where I can store everykind of object? doubles, ints, float, class objects? Or is there any method for it?
Jul 24 2014
On Thursday, 24 July 2014 at 19:43:23 UTC, Robert Rimoczi wrote:Hi! Can I somehow make a general array where I can store everykind of object? doubles, ints, float, class objects? Or is there any method for it?void main() { import std.variant, std.stdio; class Four { override string toString() const { return "4"; } } Variant[] anything; anything ~= Variant(1.0); anything ~= Variant(2); anything ~= Variant(3.0f); anything ~= Variant(new Four()); writeln(anything); } You'll want to read up on std.variant.Variant to understand how to use it. Look at get() in particular but there are other methods that you may need depending on how you are using it.
Jul 24 2014