First, the line of code:
store = new store[x];
should be:
store = new Category[x];
What are you trying to do with this line of code?
store[m]->(stuff);
Is this supposed to be a call to your constructor? If it is, this is not at all the way to do it.
Not withstanding my confusion, I'll try to offer some useful information:
First to be clear: a default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. So just because you've written "your own" constructor, doesn't mean that the constructor you've written isn't a "default" constructor. If you've written NO constructor for your class (default or otherwise), then the compiler will create a default constructor for you. This compiler-generated constructor takes no arguments, has an empty constructor initializer list and an empty body.
For the following, I'm assuming you have indeed written a constructor that is NOT a default constructor.
When an array of objects is instantiated either on the heap via a call to operator new (as you have done) or on the stack; the default constructor is called for each object in the array. If you've written a non-default constructor for your class and have NOT written a default constructor for your class, then the compiler won't generate a default constructor for you and you're going to end up with a compile error for your call to operator new. You MUST have a default constructor if you intend to create your array of objects this way. Furthermore, if you do create a default constructor for your class (so that your call to new Category[x] compiles) then when this call returns all of your objects will be constructed already; and it's a no-no to invoke a constructor on an object that's already been constructed.
You have a few choices:
1) Use a default constructor followed up by setter methods.
2) Allocate an array of pointers to stores, and then allocating a single store at a time:
1 2 3
|
Category **store = new Category*[x];
for(int m=0;m<x;m++)
store[m] = new Category(stuff);
| |
Then of course the syntax to, say, invoke a method f() on an object in your array becomes: store[i]->f() instead of store[i].f().
3) Use placement new. This involves a slightly cumbersome process of first allocating a chunk of memory big enough to hold your array and then using operator PLACEMENT new to invoke your non-default constructor for each object in your array. Likewise, the cleanup process requires the two steps of first making an explicit call to the destructor for each object in your array and then deleting the memory. This article I found explains this nicely:
http://www.devx.com/cplus/10MinuteSolution/30508/1954
4) Use a vector as suggested by ne555 above.
I hope this helps,
Matt