Vij said:
I can do this
int a [] = { 5,6,7,8,9};
but how can I do this inside a class? something like this
class CTest
{
private:
int a [] // Init the array here
};
I would do it like this:
// header file
#include <vector>
class CTest
{
public:
CTest();
private:
std::vector<int> a;
}
//source file
namespace{
// solution 1 for short arrays:
std::vector<int> init_a()
{
std::vector<int> result(5);
a.at(0) = 5;
//...
a.at(4) = 9;
return result;
}
// solution 2 for long array:
std::vector<int> init_a()
{
const int a [] = {5, 6, 7, 8, 9};
return std::vector<int>(a, &a[5]); // You might have been looking for
this? vector constructor can take two iterators.
// or, to be very safe:
// return std::vector<int>(a, &a[sizeof(a) / sizeof(int) - 1]);
}
} // unnamed namespace
CTest()
: a(init_a())
{}
- Gabriel