John Salmon wrote:
Quote:
g++ complains about illegal access to a private member
when the following is compiled with a private copy
constructor for the class C. When the copy constructor
is public, the program runs and demonstrates(?) that
the copy constructor is never called (at least no sign
of its chatter on std::cout is visible).
>
So - is it necessary to have a public copy constructor
in order to use "function style" initializers for an
array of objects? Or is this a bug in g++?
>
[jsalmon@river c++]$ cat arrayinitializer.cpp
#include <iostream>
>
struct C{
int i;
>
C(int i_) : i(i_){
std::cout << "integer ctor(" << i << ") invoked\n";
}
>
// Making the copy constructor private gets complaints
// from gcc:
// staticinitializer.cpp:17: error: 'C::C(const C&)' is private
// staticinitializer.cpp:25: error: within this context
// But leaving it public demonstrates(?) that it
// is never called. The integer ctor is used, so why the
complaint? #ifdef PRIVATE_COPY_CTOR
private:
#endif
C(const C& from) : i(from.i) {
std::cout << "copy ctor(" << from.i << ") invoked\n";
}
>
};
>
static C arr[] = {C(11), // integer ctor
C(3), // integer ctor
C(5)}; // integer ctor
>
int main(int argc, char **argv){
return 0;}
[..]
You should probably try online test drive of Comeau C++ to verify
whether certain things are valid/legal. It's not completely bug-
free, but it's damn close.
In your case, yes, you need an *accessible* copy-constructor to
initialise that array unless it is declared a member of the same
class. The simpler way to see what you're doing is
C val = 666;
or
C val(C(666));
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask