-
September 27th, 2010, 10:26 AM
#1
std::array in constructor
How dos one construct an std::array in a constructor initializer sequence?
Code:
myClass
{
public:
myClass()
: array_of_2_ints (1, 2) //<- not working...
{ }
private:
::std::array<int, 2> array_of_2_ints
}
I've tried all permutations of () and {} I could think of...
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 27th, 2010, 01:35 PM
#2
Re: std::array in constructor
The initializer list is supposed to work but I don't know if gnu has implemented such an experimental feature or not. A constructor initializer sequence is still under debate, is it not? (somebody keep me updated please)
Anyhow, as it stands for now, just default initialize it and use the constructor body, or as it is with the case with c++0x, copy it from the automatic object.
Code:
myClass
{
public:
myClass()
: array_of_2_ints (array<int, 2>{1, 2}) //<- meh...
{
// or do it here
array_of_2_ints.fill(666);
}
private:
::std::array<int, 2> array_of_2_ints;
};
If you look at the boost::array, the whole ctor/ctor/dtor is done through the operator=.
I never used boost array to an extend where I can participate in a debate over it, but the simple notion that arrays can be assigned/copied makes me go "What? you can do that?"
I don't know...
-
September 27th, 2010, 06:02 PM
#3
Re: std::array in constructor
According to this documentation there is no appropriate constructor for what you are trying to do. Where did you see the documentation that indicates that there is something other than a default constructor or copy constructor? It all looks consistent to me. You cannot provide values for elements to the constructor. You either use the initializer list or you use an algorithm within the constructor body. Unfortunately for class attributes I don't think that there is a way to use an initializer list for the array unless the array is static.
http://msdn.microsoft.com/en-us/library/bb983093.aspx
http://en.wikipedia.org/wiki/Array_%28C%2B%2B%29
Last edited by kempofighter; September 27th, 2010 at 06:06 PM.
-
September 28th, 2010, 05:08 AM
#4
Re: std::array in constructor
Originally Posted by monarch_dodra
How dos one construct an std::array in a constructor initializer sequence?
Code:
myClass
{
public:
myClass()
: array_of_2_ints (1, 2) //<- not working...
{ }
private:
::std::array<int, 2> array_of_2_ints
}
I've tried all permutations of () and {} I could think of...
If I have to init a vector with different values, I normally do something like the following:
Code:
class myClass
{
static int def_array[2];
public:
myClass()
: array_of_2_ints (&def_array[0], &def_array[2]) {}
{ }
private:
::std::array<int, 2> array_of_2_ints
}
// myClass.cpp
int myClass::def_array[2] = { 0, 1 };
I would assume that also works with std::array (which is not available in my current environment).
-
September 28th, 2010, 06:35 AM
#5
Re: std::array in constructor
Thanks for your replies. I thought array had some sort of magic that would allow this, and I though the syntax was eluding me.
Guess I'll just initialize it in the constructor, no big deal.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|