non-static const array class-member

Discussion in 'C++' started by zoe, Dec 12, 2003.

  1. zoe

    zoe Guest

    Is it possible to initialize a non-static const array class-member of
    a fundamental type?

    Something like:

    class Foo{}{
    public:
    Foo();
    private:
    const int myArray[];
    };

    Foo::Foo() : myArray({1,2,3}) {}

    this does not work of course. But is it possible in some other way?

    regards
    Zoe
     
    zoe, Dec 12, 2003
    #1
    1. Advertising

  2. zoe

    Ron Natalie Guest

    "zoe" <> wrote in message
    news:...
    > Is it possible to initialize a non-static const array class-member of
    > a fundamental type?


    No, there is no such thing as an array literal.
    You can't use the aggregate initalizer (the { .. } stuff)
    other than after the = in a declaration.
     
    Ron Natalie, Dec 12, 2003
    #2
    1. Advertising

  3. zoe

    Xenos Guest

    "zoe" <> wrote in message
    news:...
    > Is it possible to initialize a non-static const array class-member of
    > a fundamental type?
    >
    > Something like:
    >
    > class Foo{}{
    > public:
    > Foo();
    > private:
    > const int myArray[];
    > };
    >
    > Foo::Foo() : myArray({1,2,3}) {}
    >
    > this does not work of course. But is it possible in some other way?
    >
    > regards
    > Zoe


    How about:

    class Foo {
    public:
    struct Array {
    int a[N];
    };

    Foo();

    private:
    const Array myArray;
    };

    Foo::Foo(const Array& a) : myArray(a) {}


    for some constant N, or make Foo a template class as in
    template<typename N>
    Foo
    .....
     
    Xenos, Dec 12, 2003
    #3
    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. Replies:
    6
    Views:
    643
    Jean-Claude Arbaut
    Jun 16, 2005
  2. Replies:
    5
    Views:
    710
    Mark P
    Apr 4, 2006
  3. Javier
    Replies:
    2
    Views:
    603
    James Kanze
    Sep 4, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,377
    Pete Becker
    Dec 5, 2007
  5. fungus
    Replies:
    13
    Views:
    927
    fungus
    Oct 31, 2008
Loading...

Share This Page