468,513 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

initialization of a static member array of objects

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
Aug 11 '06 #1
2 4610
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
Aug 11 '06 #2
Mark P posted:
Is there another way to initialize this
array without defining the copy ctor?

Short answer: No.

Long answer: Yes, but it's verbose.

This is a defect in the C++ programming language. This may interest you:
http://groups.google.ie/group/comp.s...40c08d2?hl=en&

--

Frederick Gotham
Aug 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by cppaddict | last post: by
4 posts views Thread by Agoston Bejo | last post: by
3 posts views Thread by Tran Tuan Anh | last post: by
16 posts views Thread by Steven T. Hatton | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.