Re: Initialization of an array
* jamx:
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.no> wrote:
* Victor Bazarov:
jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
You cannot.
Well, you can zero-initialize it.
Thats what i want, to initialize them with zero ;-)
Please don't quote signatures -- corrected.
In standard C++ you just write
struct SomeClass
{
SomeClass(): some_array() {}
int some_array[2];
};
Some compilers (notably old Visual C++, IIRC) don't support that, i.e.
they're not standard-conforming, but for such compilers you can employ
the artificial base class trick:
struct SomeClassMembers
{
int some_array[2];
};
struct SomeClass: SomeClassMembers
{
SomeClass(): SomeClassMembers() {}
};
However, no matter whether your compiler is standard-conforming in this
respect or not, you're probably much better off using a std::vector than
a raw array, i.e.
struct SomeClass
{
SomeClass(): some_array(2) {}
std::vector<int> some_array;
};
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?