Array initializer in C++

Discussion in 'C++' started by Wu, Dec 14, 2005.

  1. Wu

    Wu Guest

    Hi there,

    I was wondering whether there is a way to use array initializer syntax to
    initialize a non-static array member in a class in C++. In particular, if
    I have a class Foo:

    class Foo {
    double arr[2];
    }

    How can I use C-like array intializer syntax:

    double arr[2] = {1,2}

    for the class?

    Any help is appreciated.

    --Peter
     
    Wu, Dec 14, 2005
    #1
    1. Advertising

  2. Wu

    mlimber Guest

    Wu wrote:
    > Hi there,
    >
    > I was wondering whether there is a way to use array initializer syntax to
    > initialize a non-static array member in a class in C++. In particular, if
    > I have a class Foo:
    >
    > class Foo {
    > double arr[2];
    > }
    >
    > How can I use C-like array intializer syntax:
    >
    > double arr[2] = {1,2}
    >
    > for the class?
    >
    > Any help is appreciated.
    >
    > --Peter


    You cannot use that syntax with non-static members unless the data is
    public and there are no constructors. Of course, constructors are to be
    preferred (cf. http://www.parashift.com/c -faq-lite/ctors.html). Also,
    arrays are evil
    (http://www.parashift.com/c -faq-lite/containers.html#faq-34.1). See
    this post for two similar methods for initializing std::vectors
    instead:

    http://groups.google.com/group/comp.lang.c /msg/e2fe5982913d4414

    Cheers! --M
     
    mlimber, Dec 14, 2005
    #2
    1. Advertising

  3. Wu

    Wu Guest

    Then basically I cannot have a non-static const array member in a class in C++? For example,

    class Foo {
    const double arr[2];
    }

    A 'const' array has to be intialized using initializer and a 'const'
    non-static member requires a class to have a constructor. And then with
    the information you gave, you really cannot have a non-static const
    array member in a class in C++.

    Is this right? Thanks!

    On Wed, 14 Dec 2005, mlimber wrote:

    > Wu wrote:
    > > Hi there,
    > >
    > > I was wondering whether there is a way to use array initializer syntax to
    > > initialize a non-static array member in a class in C++. In particular, if
    > > I have a class Foo:
    > >
    > > class Foo {
    > > double arr[2];
    > > }
    > >
    > > How can I use C-like array intializer syntax:
    > >
    > > double arr[2] = {1,2}
    > >
    > > for the class?
    > >
    > > Any help is appreciated.
    > >
    > > --Peter

    >
    > You cannot use that syntax with non-static members unless the data is
    > public and there are no constructors. Of course, constructors are to be
    > preferred (cf. http://www.parashift.com/c -faq-lite/ctors.html). Also,
    > arrays are evil
    > (http://www.parashift.com/c -faq-lite/containers.html#faq-34.1). See
    > this post for two similar methods for initializing std::vectors
    > instead:
    >
    > http://groups.google.com/group/comp.lang.c /msg/e2fe5982913d4414
    >
    > Cheers! --M
    >
    >
     
    Wu, Dec 14, 2005
    #3
  4. Wu

    mlimber Guest

    Wu wrote:
    > On Wed, 14 Dec 2005, mlimber wrote:
    >
    > > Wu wrote:
    > > > Hi there,
    > > >
    > > > I was wondering whether there is a way to use array initializer syntax to
    > > > initialize a non-static array member in a class in C++. In particular, if
    > > > I have a class Foo:
    > > >
    > > > class Foo {
    > > > double arr[2];
    > > > }
    > > >
    > > > How can I use C-like array intializer syntax:
    > > >
    > > > double arr[2] = {1,2}
    > > >
    > > > for the class?
    > > >
    > > > Any help is appreciated.
    > > >
    > > > --Peter

    > >
    > > You cannot use that syntax with non-static members unless the data is
    > > public and there are no constructors. Of course, constructors are to be
    > > preferred (cf. http://www.parashift.com/c -faq-lite/ctors.html). Also,
    > > arrays are evil
    > > (http://www.parashift.com/c -faq-lite/containers.html#faq-34.1). See
    > > this post for two similar methods for initializing std::vectors
    > > instead:
    > >
    > > http://groups.google.com/group/comp.lang.c /msg/e2fe5982913d4414
    > >
    > > Cheers! --M
    > >
    > >

    > Then basically I cannot have a non-static const array member in a class in C++? For example,
    >
    > class Foo {
    > const double arr[2];
    > }
    >
    > A 'const' array has to be intialized using initializer and a 'const'
    > non-static member requires a class to have a constructor. And then with
    > the information you gave, you really cannot have a non-static const
    > array member in a class in C++.
    >
    > Is this right? Thanks!
    >


    Please put your response below the text you are responding to.
    Top-posting is considered impolite. (I fixed it here.)

    You are correct that you cannot do that innately. You could, however,
    get around the limitation by using a const std::vector as in my
    previously cited post or by using a boost::scoped_array and the
    appropriate initializer like that given in the previously cited post.

    Cheers! --M
     
    mlimber, Dec 14, 2005
    #4
  5. Wu wrote:
    > Then basically I cannot have a non-static const array member in a class in C++? For example,
    >
    > class Foo {
    > const double arr[2];
    > }
    >
    > A 'const' array has to be intialized using initializer and a 'const'
    > non-static member requires a class to have a constructor. And then with
    > the information you gave, you really cannot have a non-static const
    > array member in a class in C++.
    >
    > Is this right?


    (a) Don't top-post.

    (b) Yes, this is right.

    V
     
    Victor Bazarov, Dec 14, 2005
    #5
  6. Wu

    Wu Guest

    mlimber wrote:

    > Wu wrote:
    > > On Wed, 14 Dec 2005, mlimber wrote:
    > >
    > > > Wu wrote:
    > > > > Hi there,
    > > > >
    > > > > I was wondering whether there is a way to use array initializer syntax to
    > > > > initialize a non-static array member in a class in C++. In particular, if
    > > > > I have a class Foo:
    > > > >
    > > > > class Foo {
    > > > > double arr[2];
    > > > > }
    > > > >
    > > > > How can I use C-like array intializer syntax:
    > > > >
    > > > > double arr[2] = {1,2}
    > > > >
    > > > > for the class?
    > > > >
    > > > > Any help is appreciated.
    > > > >
    > > > > --Peter
    > > >
    > > > You cannot use that syntax with non-static members unless the data is
    > > > public and there are no constructors. Of course, constructors are to be
    > > > preferred (cf. http://www.parashift.com/c -faq-lite/ctors.html). Also,
    > > > arrays are evil
    > > > (http://www.parashift.com/c -faq-lite/containers.html#faq-34.1). See
    > > > this post for two similar methods for initializing std::vectors
    > > > instead:
    > > >
    > > > http://groups.google.com/group/comp.lang.c /msg/e2fe5982913d4414
    > > >
    > > > Cheers! --M
    > > >
    > > >

    > > Then basically I cannot have a non-static const array member in a class in C++? For example,
    > >
    > > class Foo {
    > > const double arr[2];
    > > }
    > >
    > > A 'const' array has to be intialized using initializer and a 'const'
    > > non-static member requires a class to have a constructor. And then with
    > > the information you gave, you really cannot have a non-static const
    > > array member in a class in C++.
    > >
    > > Is this right? Thanks!
    > >

    >
    > Please put your response below the text you are responding to.
    > Top-posting is considered impolite. (I fixed it here.)
    >
    > You are correct that you cannot do that innately. You could, however,
    > get around the limitation by using a const std::vector as in my
    > previously cited post or by using a boost::scoped_array and the
    > appropriate initializer like that given in the previously cited post.
    >
    > Cheers! --M


    Hi mlimber, Victor and all,

    Sorry for the top-posting. I didn't mean it (I am new to newsgroup, so I
    didn't know this convention).

    Thanks a lot for your quick responses!

    --Peter
     
    Wu, Dec 14, 2005
    #6
    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. Steve
    Replies:
    1
    Views:
    382
    Victor Bazarov
    Jan 25, 2004
  2. Steve
    Replies:
    2
    Views:
    371
    Steve
    Jan 24, 2004
  3. Alexander Stippler

    array initializer / conversion to pointer

    Alexander Stippler, May 27, 2004, in forum: C Programming
    Replies:
    12
    Views:
    631
    E. Robert Tisdale
    May 30, 2004
  4. Leo Havmøller

    Sized array w/initializer

    Leo Havmøller, Apr 21, 2010, in forum: C Programming
    Replies:
    4
    Views:
    370
    Gil Johnson
    Apr 22, 2010
  5. luser- -droog

    discover array offsets within initializer?

    luser- -droog, Jan 29, 2011, in forum: C Programming
    Replies:
    2
    Views:
    299
    luser- -droog
    Jan 30, 2011
Loading...

Share This Page