-3

Possible Duplicate:
c++ Object array initialization without default constructor

I have structure:

struct aa 
{
    int a;
    char  b [255];

    aa(int number)
    {
        cout<<"creating structure "<<number<<"\n";

    }

    ~aa()
    {
        cout<<"destroying structure"; 
    }
};

I'm trying to create array of this structure, but this doesn't work:

aa *ss = new aa[3](1);

This gives me a compiler error.

How do I do it?

Community
  • 1
  • 1
vico
  • 17,051
  • 45
  • 159
  • 315
  • You forgot to ask a question. But the answer is probably, "don't use arrays, use some sane collection like a vector". – David Schwartz Jul 22 '12 at 12:55
  • I think it's fairly clear what the question should have been. I've edited the post to add the implied question. – Mark Byers Jul 22 '12 at 12:57
  • You should use use `std::vector`. If you can't for some reason, [this answer](http://stackoverflow.com/a/2493450/59379) covers the alternatives. – JoeG Jul 22 '12 at 13:02
  • Yeas, I know about vector, but I'm trying to understand language specific. I have feeling that nobody uses simple arrays even in simple applications - is it true? – vico Jul 22 '12 at 13:02
  • Some use them to do really simple things. – Morwenn Jul 22 '12 at 13:25

1 Answers1

0
aa(int number=1)
・・・
aa *ss = new aa[3];

or

template <int N>
struct aa
・・・
aa(int number = N)
・・・
aa<1> *ss = new aa<1>[3];

or

#include <new>
・・・
aa(int number=0)
・・・

int main(){
    aa ss[3];//local value
    for(int i=0;i<3;++i)
        new (ss + i) aa(1);//replacement
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70