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?
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));
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.
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.
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);
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.
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;
}
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.
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);
}
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.
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?
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.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor
Related
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?
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));
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.
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.
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);
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.
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;
}
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.
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);
}
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.
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?
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.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor
I don't understand the new C++11 syntax yet for initializing an array in a constructor initilizer list. I'm no longer stuck with C++03 but I can't use boost or std::vector because of program constraints.
An instance of FOO must be sized by the constructor call and behave as though the sizes of x and y were statically known. Does the new C++11 features allow this?
I'm not sure if or how std::initializer_list<> can help.
class FOO
{
public:
// this constructor needs to size x = count and y = count * 4
FOO(int count) :
{
// interate over x and y to give them their initial and permenent values
}
private:
const BAR x[];
const TAR y[];
};
#include "foo.h"
void main(void)
{
// both need to work as expected
FOO alpha(30);
FOO * bravo = new FOO(44);
}
You can't do what you're trying to do. The sizes of arrays must be compile-time constants. And while the values provided to the constructors in your particular use cases may be compile-time constants, C++ can't know that.
Furthermore, as a statically-typed language, C++ requires being able to compute the size of the class at compile-time. sizeof(Foo) needs to have an exact, single value. And your's can't.
Initializer lists aren't going to help you. You want two runtime-sized arrays; that's what std::vector is for. If you want compile-time sized arrays, you need to use a template type:
template<int count>
class FOO
{
public:
FOO(initializer_list<Whatever> ilist)
{
// interate over x and y to give them their initial and permenent values
}
private:
const BAR x[count];
const TAR y[count * 4];
};
#include "foo.h"
void main(void)
{
// both need to work as expected
FOO<30> alpha;
FOO<44> * bravo = new FOO<44>;
}
Besides the answer from Nicol Bolas using template parameters to make the size compile-time configurable, you can also allocate memory on the heap:
class FOO
{
public:
// this constructor needs to size x = count and y = count * 4
FOO(int count) : x(new BAR[count]), y(new TAR[count])
{
// interate over x and y to give them their initial and permenent values
}
// Need a destructor to free the memory we allocated in the constructor
~FOO()
{
delete [] y;
delete [] x;
}
private:
const BAR* x;
const TAR* y;
};
Please consider the following example
struct Foo
{
int bar;
Foo(int i):bar(i){cout << "real ctor\n";}
Foo(){cout << "default ctor\n";}
};
int main()
{
Foo fooArr[3];//default ctor called 3 times
for(int i=0;i!=3;++i)cout << fooArr[i].bar << endl;//bare memory junk
cout << endl;
vector<Foo> fooVec;
for(int i=0;i!=3;++i){
fooVec.push_back(Foo(i)); //only real ctor called
cout << fooVec[i].bar << endl;//real thing
}
cout << endl;
int iArr[3];
for(int i=0;i!=3;++i)cout << iArr[i] << endl;//bare memory junk
}
I don't want any user of Foo to call its default constructor, because it's not in my design. But I'd like my users to be able to use an array of Foo, to support that, I was forced to provide a pointless and confusing Foo::Foo(). I just don't understand why does the C++ standard force programmers to do such a thing. What is the rationale behind it? Why the inconsistency? Could any of you smart guys who get this explain it to me, please? Thanks in advance!
You can make arrays of Foo even if it doesn't have a default constructor. It's just that the elements have to be constructed when you declare the array. So you can do this:
Foo fooArr[] = { Foo( 1 ), Foo( 2 ), Foo( 3 ) };
The alternative is to use a a dynamic array (your vector<Foo> example, which is probably best) or an array of pointers to Foo (like shared_ptr<Foo> arrFoo[3])
shared_ptr<Foo> arrFoo[3];
arrFoo[2].reset( new Foo(3) );
A final note about vector<Foo>: since the size of the array is known in advance, you can improve performance by reserving enough space in the vector for all future Foos:
vector<Foo> arrFoo;
arrFoo.reserve( 3 );
for( int i = 0; i<3; ++i )
arrFoo.push_back( Foo( i ) );
EDIT: Your question was why do you have to have a default constructor to make a static array of the type. I thought the answer was clear but I'll try to explain it.
Foo bar1; Foo bar2; creates two objects using the default constructor, since no arguments were provided.
Foo bar[2]; is essentially the same thing. It declares two objects that need to be constructed. There is no way to declare an object without constructing it - that's the very point of declaring it in the first place.
A static array in C++ is just a bunch of objects placed contiguously memory. It's not a separate object.
Hope that makes sense.
The rationale is that the array is full of default constructed elements, so the type of the elements must be default constructible. If you initialized the array with some values, the default construction wouldn't be required:
Foo fooArr1[3]; // full of default constructed Foos
Foo fooArr[3] = {1,2,3}; // default constructor not required. Foo(int) called.
Note that the second line in the code example uses the implicit conversion from int to Foo provided by the implicit Foo(int) converting constructor.
The reason you have to provide your own default constructor is that you have declared one constructor, which disables the automatic generation of the default constructor. The rationale behind this is that if you need to provide some constructor, it is likely that you also want to do something special in the default constructor.
If you really are worried about user provided constructors, then you can make your class a real aggregate and use aggregate initialization:
struct Foo
{
// no user declared constructors
int foo;
};
int main()
{
Foo fooArr1[3]; // OK
Foo fooArr[3] = { {1}, {2}, {3} }; // aggregate construction
}
In C++11 you can enable the compiler generated default constructor using default:
Foo()=default;
You have to choose: either not defining a default constructor, and therefore, you can't declare an array of Foo. Or declaring a default constructor (empty even) and can declare an array of Foo.
If you have dealt before with OOP languages such as C# or Java, and you have a class Foo and Foo[] arr, then you don't have to declare default constructor, because the array in these languages carries only references (addresses) to objects. The array itself is an object, so arr when created will == null. When using arr = new Foo[3]; then we make a new object of array that contains 3 references: arr == { null, null, null }. Then you assign an object to each reference: for (int i = 0; i < 3; ++i) arr[i] = new Foo(i);.
However, C++ is different because the arrays carry the object itself rather than a reference to it. So, when carrying the object itself, it must have no-parameter constructor to be called with each object. (i.e. in C++: Foo arr[3]; then arr = { objectOfFoo, objectOfFoo, objectOfFoo }
A solution to your problem may found by decalring an array of pointers:
Foo * arr[10] = { 0 }; // arr = { NULL, NULL, NULL, ... , NULL }
for (int i = 0; i < 10; ++i) arr[i] = new Foo(3); // you don't have to declare default constructor
// some using of array
// C++ doesn't have a garbage collector
for (int i = 0; i < 10; ++i) delete arr[i];
Why do I have to provide default ctor if I want to create an array of objects of my type?
Thanks for answers
Because they have to be initialized.
Consider if it wasn't the case:
struct foo
{
foo(int) {}
void bar(void) {}
};
foo a[10];
foo f = a[0]; // not default-constructed == not initialized == undefined behavior
Note you don't have to:
int main(){
// initializes with the int constructor
foo a[] = {1, 2, 3};
}
// if the constructor had been explicit
int main(){
// requires copy-constructor
foo a[] = {foo(1), foo(2), foo(3)};
}
If you really need an array of objects and you can't give a meaningful default constructor, use std::vector.
If you really need an array of of objects, can't give a meaningful default constructor, and want to stay on the stack, you need to lazily initialize the objects. I have written such a utility class. (You would use the second version, the first uses dynamic memory allocation.)
For example:
typedef lazy_object_stack<foo> lazy_foo;
lazy_foo a[10]; // 10 lazy foo's
for (size_t i = 0; i < 10; ++i)
{
// create a foo, on the stack, passing `i` to the constructor
a[i].create(i);
}
for (size_t i = 0; i < 10; ++i)
a[i].get().bar(); // and use it
// automatically destructed, of course
The default constructor will be called for each object in the array.
You don't have to specify a default constructor as one will be created for you.
Just make sure you don't declare a constructor with no parameters as private or protected.
Here's an example of one being created for you:
class C
{
};
C c[10];
Whereas if you make it private you get a compiling error:
class C
{
private:
C()
{
}
};
C c[10];
When defining an array, you cannot specify ctor parameter, hence every object in the array must be constructed using the default ctor.
Normally, the C++ compiler will create the default ctor for you automatically. But, if you define a ctor with parameters, then the automatically creation of the default ctor is supressed, and you must explicitly write one.
Because when the array is initialized, default constructors are called for its items.
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?
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));
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.
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.
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);
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.
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;
}
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.
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);
}
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.
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?
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.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor