tags:

views:

312

answers:

11

If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class

class Foo{

public:
Foo(int i) {}

}

How to declare an array or a vector which contains 10000 Foo objects?

+2  A: 

You'd need to do an array of pointers to Foo.

Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
    myArray[i] = new Foo(i);
Cory Petosky
In most cases this would not be a solution (i.e. Foo objects required to be contiguous). This changes the scenario much more than using an `std::vector` in the first case.
David Rodríguez - dribeas
Agreed. I completely missed the word 'vector' in his final sentence. I thought his 'I know vectors are recommended' was trying to say 'I know vectors are the right solution, but I want an array anyway'.
Cory Petosky
+6  A: 

Actually, you can do it as long you use an initialization list, like

Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };

however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.

fogo
You can type a little less (if the constructor is not explicit): `Foo foos[] = { 0, 1, 2, 3 };`
David Rodríguez - dribeas
As you write, that's impractical. However, skydoor asked about array or vector.
sbi
@David Rodríguez - dribeas Oh sure! I know about that. But for the sake of the example I prefered to be explicit. @sbi I understand your point. Maybe I should have said considering how impratical it is to use a vector instead.
fogo
+1  A: 

When you declare an object that has no default constructor, you must initialize it in the declaration.

Foo a; // not allowed
Foo b(0); // OK

The same goes for arrays of such types:

Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK

In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.

Rob Kennedy
I wonder why people always insist on making an explicit cast between the `{}`...
AndreyT
I don't insist, @Andrey. I just don't always remember whether implicit conversions occur in array initializers. Edited to show both syntaxes. The explicit syntax would be required for multi-parameter constructors, but is optional for single-parameter, non-explicit constructors.
Rob Kennedy
But that's not really an option with the given 10000 objects, is it?
sbi
The only reason it's not an option, @Sbi, is if the compiler's internal limitations prevent it from compiling an initializer list as complex as one with 10,000 elements. If it's allowed, then it's an option. It's not a *practical* option, but I noted that in my answer already.
Rob Kennedy
@Rob: I see. I'm sorry I overlooked that. However, I still don't feel this really answers the question, as that was about array _or_ vector. and there _is_ a practical solution using `std::vector`.
sbi
+1  A: 

You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}

Foo array[10000] = { 1, 2, 3, ..., 10000 };

Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.

AndreyT
Note that the question was about 10000 objects. That makes for quite a big file to swallow for the compiler.
sbi
+12  A: 

For an array you would have to provide an initializer for each element of the array at the point where you define the array.

For a vector you can provide an instance to copy for each member of the vector.

e.g.

std::vector<Foo> thousand_foos(1000, Foo(42));
Charles Bailey
Downvoter, care to suggest improvement / correction?
Charles Bailey
For plain array, I'm just sayin' you don't *have* to do it that way is all... seems misleading. There are other, practical, alternatives. Plus, the vector approach shown here doesn't allow the constructor to be called with different arguments.
Dan Moulding
@Dan Moulding: But you can't _define_ an array of a class type with no default constructor without providing initializers. I stand by this statement. The question asks for an array or a vector and doesn't give any guidance as to the values needed so I still don't think that my answer is incorrect or unhelpful even if there are other possibilities.
Charles Bailey
Fair enough. I suppose if you're talking about array *types* that's true. I was thinking more in terms of arrays as a contiguous sequence of objects, in which case... well there are alternatives. I'd take back my downvote, but to my surprise, SO won't let me because my vote is now "too old" -- eh???
Dan Moulding
@Dan, please consider http://meta.stackoverflow.com/questions/30470/mistaken-downvote-cannot-be-undone-after-matter-is-being-clarified-by-the-answere
Johannes Schaub - litb
@Dan Moulding: I've made a minor clarifying edit now.
Charles Bailey
@litb: thanks for the pointer. I've not heard of this stuff before (I never go to meta). Seems like it would just be better to not allow voting on answers to questions you've also answered, wouldn't it? Considering there's a potential COI and all (I'm, of course, guilty of that here, but not for any nefarious, tactical, or other selfish reasons -- I just honestly thought the answer was misleading and was upvoted more than it deserved).
Dan Moulding
Good that 10K+ users can see how many active downvotes an answer has :)
Johannes Schaub - litb
+1  A: 

The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.

You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:

#include <iostream>
#include <vector>

struct foo {
    foo(int) {}
};

int main()
{
    std::vector<foo> v;
    v.resize(10000,foo(42));
    std::cout << v.size() '\n';
    return 0;
}
sbi
*Why* is it better to use `std::vector` if they do the same thing? Because of the 50% increase in code size you get from using STL? ;)
Dan Moulding
@Dan: It's better because it avoids common errors. (And that size increase argument is just plain FUD.)
sbi
Plain FUD? On one embedded system I'm working on, using just *one* instance of a vector made my shared library size jump from ~100KiB to well over 1MiB. That's not FUD. And, in this case, it's an increase that I can't afford. This is with GCC.
Dan Moulding
@Dan: Well, so you're on an embedded system. Don't get me wrong, but IME most people asking here aren't. For most 1MB isn't an issue, while shipping bug free software certainly is.
sbi
@sbi: Well, that's one thing that really irks me. IME software people have a tendency to have a very narrow software world-view. Most think that Linux/Windows/Mac desktop apps and Web development is all there is. It turns out there's actually a *whole lot* of people who do embedded work. It's *not* a niche.
Dan Moulding
@Dan: I haven't said it's a niche. I said that most people asking questions here are programming for desktop systems. And if they aren't, they usually say that they are working in the embedded field, because they feel it needs mentioning. (In this case, however, skydoor actually even _asked_ about how to do this with a vector.) Anyway, on embedded systems, special rules apply, that many mainstream suggestions don't consider. Heck, on some of them you have to watch the stack size or don't even have free storage. You hadn't suggested that either.
sbi
@Dan: Here's a nice article on the subject: http://www.joelonsoftware.com/articles/fog0000000020.html
sbi
@sbi: That's not a bad article from a desktop software perspective. The irony of that is not lost on me ;) Two things: a) the answers here on SO aren't intended only for the person asking the question, but are also (maybe primarily, even) for the people who come searching for answers *later*. Would it be helpful for an embedded developer who has this question to come here and find answers that are only suitable for desktop environments? b) It's best for library developers to consider *all* the systems on which their code might be useful. Being conservative can be important. +1, BTW :)
Dan Moulding
I guess my main point is, one isn't necessarily better than the other. As usual, there are tradeoffs. That's all I'm sayin'.
Dan Moulding
@Dan: I can fully agree to your point that there are tradeoffs. `:)`
sbi
A: 

Another option might be to use an array of boost::optional<Foo>:

boost::optional<Foo> foos[10]; // No construction takes place
                               // (similar to vector::reserve)

foos[i] = Foo(3); // Actual construction

One caveat is that you'll have to access the elements with pointer syntax:

bar(*foos[2]); // "bar" is a function taking a "Foo"

std::cout << foos[3]->baz(); // "baz" is a member of "Foo"

You must also be careful not to access an unitialized element.

Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.

Manuel
Nice idea, but requires `Foo` to have a copy constructor doesn't it?
Mark Ransom
@Mark - Good point. But you can bypass this limitation with a little trick: http://codepad.org/csde4mwT
Manuel
A: 

In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?

bta
In C++ the remaining array members are *value-initialized*, which in this case would indeed require a default constructor.
AndreyT
+4  A: 

sbi had the best answer for plain arrays, but didn't give an example. So...

You should use placement new:

char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
    new (fooArray + i) Foo(i); // Call non-default constructor
}

Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:

// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
    fooArray[i].~Foo();
}

// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);

This is about the only time you ever see a legitimate explicit call to a destructor.

NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.

Dan Moulding
I'm not downvoting as there isn't anything fundamentally wrong with the approach, but you can get the same effect with `std::vector<Foo>`, `reserve` and `push_back` and it's a lot easier to make it robust and exception safe.
Charles Bailey
Yup. When I wrote this, I had apparently overlooked the "or vector" at the end of the question, and was focusing entirely on the question as summed up in the title. That being the case, this here is quite possibly the *only* practical way to do it. I certainly wouldn't say this is better than using a vector, but if you *need* a plain array...
Dan Moulding
There is no need to call the destructors manually. In fact, this is harmful, because `delete[] fooArray` already does that before realeasing the memory. Also, I would replace the first two lines with `Foo* fooArray = reinterpret_cast<Foo*>(operator new(10000 * sizeof(Foo)))`.
FredOverflow
@FredOverflow: Ooops. You caught a bug. I forgot to cast it back to an array pointer before deleting it. Attempting `delete [] fooArray` would cause the compiler to attempt to destroy `sizeof(foo) * 10000` Foo objects, which is *obviously* not right. It's important to always delete the same type of object(s) that are newed. I newed `sizeof(Foo) * 10000` chars and I must delete `sizeof(Foo) * 10000` chars. This is, of course, why you *do* need to explicitly call the Foo destructors. Fixed.
Dan Moulding
@Dan: What's wrong with `delete[] place;`?
sbi
@sbi: `delete [] place` works too, so long as `place` is still in scope. My example didn't make this very clear, but I was assuming that the cleanup code was in a different context and that therefore, `place` may no longer be in scope (you could of course save "place", but then you must save two pointers instead of just one).
Dan Moulding
@Dan: I see. The requirement of having to store the pointer is certainly more demanding than just having to know its type. I didn't think of it that way.
sbi
A: 

If it makes sense for your class, you could provide a default value for your constructor parameter:

class Foo
{ 
public: 
  explicit Foo(int i = 0); 
}

Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)

I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.

Bill
+1  A: 
class single
{
    int data;
public:
    single()
    {
        data = 0;
    }
    single(int i)
    {
        data = i;
    }
};

// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++) 
{
    obj[z] = new single(10);
}
Rajendra Kumar Uppal