initialization of a static member array of objects

Discussion in 'C++' started by Mark P, Aug 11, 2006.

  1. Mark P

    Mark P Guest

    class A
    {
    private:
    explicit A( int i ) {}
    A( const A& );

    public:
    static const A arr[2];
    };

    const A A::arr[] = { A(0), A(1) };

    int main ()
    {}

    Consider the above code and note that the copy ctor for A has been
    declared but not defined. gcc reports a link error to the copy ctor
    which I assume means that it's trying to initialize "arr" by copying the
    temporaries A(0) and A(1). Is there another way to initialize this
    array without defining the copy ctor?

    -Mark
    Mark P, Aug 11, 2006
    #1
    1. Advertising

  2. Mark P wrote:
    > class A
    > {
    > private:
    > explicit A( int i ) {}
    > A( const A& );
    >
    > public:
    > static const A arr[2];
    > };
    >
    > const A A::arr[] = { A(0), A(1) };
    >
    > int main ()
    > {}
    >
    > Consider the above code and note that the copy ctor for A has been
    > declared but not defined. gcc reports a link error to the copy ctor
    > which I assume means that it's trying to initialize "arr" by copying
    > the temporaries A(0) and A(1). Is there another way to initialize
    > this array without defining the copy ctor?


    No.

    If you specify '0' and '1' without wrapping them in 'A()', your
    compiler should complain because your A(int) is declared "explicit".
    If you do wrap them, like you did, each array element is copy-
    initialised, and that requires a copy c-tor. Provide one.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 11, 2006
    #2
    1. Advertising

  3. Frederick Gotham, Aug 11, 2006
    #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:
    3
    Views:
    828
    Peter_Julian
    Oct 10, 2005
  2. dolphin
    Replies:
    3
    Views:
    1,324
    Pete Becker
    Dec 5, 2007
  3. aaragon
    Replies:
    2
    Views:
    604
    James Kanze
    Nov 2, 2008
  4. Ajay 0x007
    Replies:
    3
    Views:
    1,640
    Andrey Tarasevich
    Nov 18, 2009
  5. mike.arsenault
    Replies:
    4
    Views:
    3,897
    mike.arsenault
    Nov 25, 2009
Loading...

Share This Page