std::array in constructor
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: std::array in constructor

  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,337

    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 LITE 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.

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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...

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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_&#37;28C%2B%2B%29
    Last edited by kempofighter; September 27th, 2010 at 07:06 PM.

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: std::array in constructor

    Quote Originally Posted by monarch_dodra View Post
    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).

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,337

    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 LITE 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center