Array data members initialization

Discussion in 'C++' started by iluvatar, Sep 11, 2006.

  1. iluvatar

    iluvatar Guest

    Hi all.

    How can I initialize an array data member in the "faster" way? For
    example, suppose I have a class like

    class Example{
    private:
    double array[3];
    public:
    Example(const double & val0, const double & val1, const double &
    val2); // yes, with doubles
    ...
    };

    and I want to write for the constructor something like

    Example::Example(const double & val0, const double & val1, const double
    & val2)
    : array[0](val0), array[1](val1), array[2](val2)
    {}

    but obviously it does not work for me. I dont know what is the correct
    syntax for the initialization, and if the member double array[3] MUST
    be initialized with an array and no member by member as I did. Please
    help me. Thank you. I am sorry for my English. Thank you again.
    iluvatar, Sep 11, 2006
    #1
    1. Advertising

  2. iluvatar

    Guest

    iluvatar wrote:
    > Hi all.
    >
    > How can I initialize an array data member in the "faster" way? For
    > example, suppose I have a class like
    >
    > class Example{
    > private:
    > double array[3];
    > public:
    > Example(const double & val0, const double & val1, const double &
    > val2); // yes, with doubles
    > ...
    > };
    >
    > and I want to write for the constructor something like
    >
    > Example::Example(const double & val0, const double & val1, const double
    > & val2)
    > : array[0](val0), array[1](val1), array[2](val2)
    > {}
    >
    > but obviously it does not work for me. I dont know what is the correct
    > syntax for the initialization, and if the member double array[3] MUST
    > be initialized with an array and no member by member as I did. Please
    > help me. Thank you. I am sorry for my English. Thank you again.


    Pass a reference to an array and use a reference to an array as a
    member of you class.

    class Example{
    private:
    const double (&array1) [3];
    public:
    Example( const double (&arr)[3]) : array1(arr)
    {
    }
    };

    int main(){
    const double arrd[3] = { 3.0, 2.0, 1.0};
    Example e(arrd);
    return 0;
    }
    , Sep 11, 2006
    #2
    1. Advertising

  3. iluvatar

    Howard Guest

    "iluvatar" <> wrote in message
    news:...
    > Hi all.
    >
    > How can I initialize an array data member in the "faster" way?


    Faster in what sense?

    > For example, suppose I have a class like
    >
    > class Example{
    > private:
    > double array[3];
    > public:
    > Example(const double & val0, const double & val1, const double &
    > val2); // yes, with doubles
    > ...
    > };
    >
    > and I want to write for the constructor something like
    >
    > Example::Example(const double & val0, const double & val1, const double
    > & val2)
    > : array[0](val0), array[1](val1), array[2](val2)
    > {}
    >
    > but obviously it does not work for me. I dont know what is the correct
    > syntax for the initialization, and if the member double array[3] MUST
    > be initialized with an array and no member by member as I did. Please
    > help me. Thank you. I am sorry for my English. Thank you again.
    >


    You could use assignments in the constructor body:

    Example::Example( const double & val0,
    const double & val1, const double & val2)
    {
    array[0] = val0;
    array[1] = val1;
    array[2] = val2;
    }

    -Howard
    Howard, Sep 11, 2006
    #3
  4. iluvatar posted:

    > Example::Example(const double & val0, const double & val1, const double
    > & val2)
    > : array[0](val0), array[1](val1), array[2](val2)
    > {}



    Take those parameters by value, not by reference -- a "double" doesn't
    consume enough memory to warrant passing around its address rather than its
    actual value. (Then again, your compiler might just compile as if you had
    passed by value...)

    You have stumbled across a defect in C++. When writing the C++ Standard,
    the committee members focused all of their attention on adding new features
    to the language, and neglected to refine the more basic features. We
    _should_ be able to do something akin to the following:

    class MyClass {
    private:

    double const arr[3];

    public:

    MyClass(double const a,double const b,double const c)
    : arr( {a,b,c} )
    {
    /* Function Body */
    }
    };

    , but alas we can't. The Standards Committee are apathetic when it comes to
    remedying fundamental defects of this nature, so the defect may never be
    resolved.

    A possible solution might be to use compound literals (which are a feature
    of C99):

    : arr( (double[3]){a,b,c} )

    Strictly speaking, they're not supported by C++, but most C++ compilers
    support features of C99.

    --

    Frederick Gotham
    Frederick Gotham, Sep 11, 2006
    #4
  5. iluvatar

    iluvatar Guest

    Thank you Frederick. I use the gnu g++ compiler and the arr(
    (double[3]){a,b,c} ) does not work. But the most important fact now is
    your point of the lack for options such arr( {a,b,c} ) in the actual
    standard. I will try to change my code to three data members replacing
    the array. Bye.

    Frederick Gotham wrote:
    > iluvatar posted:
    >
    > > Example::Example(const double & val0, const double & val1, const double
    > > & val2)
    > > : array[0](val0), array[1](val1), array[2](val2)
    > > {}

    >
    >
    > Take those parameters by value, not by reference -- a "double" doesn't
    > consume enough memory to warrant passing around its address rather than its
    > actual value. (Then again, your compiler might just compile as if you had
    > passed by value...)
    >
    > You have stumbled across a defect in C++. When writing the C++ Standard,
    > the committee members focused all of their attention on adding new features
    > to the language, and neglected to refine the more basic features. We
    > _should_ be able to do something akin to the following:
    >
    > class MyClass {
    > private:
    >
    > double const arr[3];
    >
    > public:
    >
    > MyClass(double const a,double const b,double const c)
    > : arr( {a,b,c} )
    > {
    > /* Function Body */
    > }
    > };
    >
    > , but alas we can't. The Standards Committee are apathetic when it comes to
    > remedying fundamental defects of this nature, so the defect may never be
    > resolved.
    >
    > A possible solution might be to use compound literals (which are a feature
    > of C99):
    >
    > : arr( (double[3]){a,b,c} )
    >
    > Strictly speaking, they're not supported by C++, but most C++ compilers
    > support features of C99.
    >
    > --
    >
    > Frederick Gotham
    iluvatar, Sep 12, 2006
    #5
  6. iluvatar posted:

    > Thank you Frederick. I use the gnu g++ compiler and the arr(
    > (double[3]){a,b,c} ) does not work.



    It works for _me_ on g++. Maybe you need to upgrade your compiler?


    > But the most important fact now is your point of the lack for options
    > such arr( {a,b,c} ) in the actual standard. I will try to change my code
    > to three data members replacing the array. Bye.



    If the array is non-const, you can simply put the code in the constructor
    body:

    MyClass::MyClass(double const a,double const b,double const c)
    {
    double *p = arr;

    *p++ = a;
    *p++ = b;
    *p = c;
    }

    --

    Frederick Gotham
    Frederick Gotham, Sep 12, 2006
    #6
  7. iluvatar

    Default User Guest

    iluvatar wrote:

    > Thank you Frederick.



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>





    Brian
    Default User, Sep 12, 2006
    #7
  8. iluvatar

    mlimber Guest

    iluvatar wrote:
    > How can I initialize an array data member in the "faster" way? For
    > example, suppose I have a class like
    >
    > class Example{
    > private:
    > double array[3];
    > public:
    > Example(const double & val0, const double & val1, const double &
    > val2); // yes, with doubles
    > ...
    > };
    >
    > and I want to write for the constructor something like
    >
    > Example::Example(const double & val0, const double & val1, const double
    > & val2)
    > : array[0](val0), array[1](val1), array[2](val2)
    > {}
    >
    > but obviously it does not work for me. I dont know what is the correct
    > syntax for the initialization, and if the member double array[3] MUST
    > be initialized with an array and no member by member as I did.


    Use a vector instead of an array (cf.
    http://www.parashift.com/c -faq-lite/containers.html#faq-34.1) and an
    initializer helper class:

    #include <vector>
    using namespace std;

    template<typename T>
    class Initializer
    {
    vector<T> v_;
    public:
    Initializer& Add( const T& t ) { v_.push_back(t); return *this; }
    operator vector<T>() const { return v_; }
    };

    class Example
    {
    const vector<double> v_;
    public:
    Example( double d0, double d1, double d2 )
    : v_( Initializer<double>()
    .Add(d0)
    .Add(d1)
    .Add(d2) )
    {}
    // ...
    };

    Cheers! --M
    mlimber, Sep 12, 2006
    #8
  9. iluvatar

    iluvatar Guest

    Ohh, sorry. I was making a mistake. The (double[3]){a,b,c} ) also works
    for me. But is more slow than the initial option. Bye

    Frederick Gotham wrote:
    > iluvatar posted:
    >
    > > Thank you Frederick. I use the gnu g++ compiler and the arr(
    > > (double[3]){a,b,c} ) does not work.

    >
    >
    > It works for _me_ on g++. Maybe you need to upgrade your compiler?
    >
    >
    > > But the most important fact now is your point of the lack for options
    > > such arr( {a,b,c} ) in the actual standard. I will try to change my code
    > > to three data members replacing the array. Bye.

    >
    >
    > If the array is non-const, you can simply put the code in the constructor
    > body:
    >
    > MyClass::MyClass(double const a,double const b,double const c)
    > {
    > double *p = arr;
    >
    > *p++ = a;
    > *p++ = b;
    > *p = c;
    > }
    >
    > --
    >
    > Frederick Gotham
    iluvatar, Sep 13, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Theese

    Initialization of data members

    Dave Theese, Sep 16, 2003, in forum: C++
    Replies:
    3
    Views:
    282
    Gianni Mariani
    Sep 16, 2003
  2. Peter Olcott
    Replies:
    4
    Views:
    764
    Peter Olcott
    May 31, 2006
  3. toton
    Replies:
    5
    Views:
    918
    Victor Bazarov
    Sep 28, 2006
  4. Lionel B
    Replies:
    5
    Views:
    352
    James Kanze
    Jul 19, 2007
  5. aaragon
    Replies:
    2
    Views:
    605
    James Kanze
    Nov 2, 2008
Loading...

Share This Page