initialization of a static member array of objects

M

Mark P

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
 
V

Victor Bazarov

Mark said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top