linux - How to allocate memory dynamically for class using new in C++? -
have following class: class mem { private: int data; public: mem(){} mem(int a) { data=a; } void datadis() { cout <<"valu of "<< data << endl; } }; how allocate memory 10 object of class using parametrized constructor , new operator? since need use new directly, can ten separate objects: mem * mem1 = new mem(42); // , on you can't specify initialisers when allocating array new ; you'll have let them default-initialised, reassign them: mem * mems = new mem[10]; mems[0] = mem(42); // , on don't forget assign them smart pointers (or delete them when you've finished them, if weird requirement use new forbids other forms of sensible memory management). when find working under less insane restrictions, use std::array or std::vector instead of mucking around raw memory allocations: std::vector<mem> mems = {42, 6...