C++ / boost : declaring an encapsulated shared_array -
i started use smart pointers. if correct, smart pointers declared:
shared_array<double> a(new double[n]);
but how if encapsulated in class ? moment doing follow, seems super ugly:
header file:
class foo { public: foo(int size); shared_array<double> _a; };
source file
foo::foo(int n){ shared_array<double> p (new double[n]); _a = p; }
you can use constructor initialization list:
foo::foo(int n) : _a(new double[n]) {}
in case need set managed array in constructor's body, then
foo::foo() { int n = somecalculation(); _a.reset(new double[n]); }
Comments
Post a Comment