J
John Salmon
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;}
[jsalmon@river c++]$ g++ arrayinitializer.cpp
[jsalmon@river c++]$ a.out
integer ctor(11) invoked
integer ctor(3) invoked
integer ctor(5) invoked
[jsalmon@river c++]$ g++ -DPRIVATE_COPY_CTOR arrayinitializer.cpp
arrayinitializer.cpp:19: error: `C::C(const C&)' is private
arrayinitializer.cpp:27: error: within this context
arrayinitializer.cpp:19: error: `C::C(const C&)' is private
arrayinitializer.cpp:27: error: within this context
arrayinitializer.cpp:19: error: `C::C(const C&)' is private
arrayinitializer.cpp:27: error: within this context
[jsalmon@river c++]$
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;}
[jsalmon@river c++]$ g++ arrayinitializer.cpp
[jsalmon@river c++]$ a.out
integer ctor(11) invoked
integer ctor(3) invoked
integer ctor(5) invoked
[jsalmon@river c++]$ g++ -DPRIVATE_COPY_CTOR arrayinitializer.cpp
arrayinitializer.cpp:19: error: `C::C(const C&)' is private
arrayinitializer.cpp:27: error: within this context
arrayinitializer.cpp:19: error: `C::C(const C&)' is private
arrayinitializer.cpp:27: error: within this context
arrayinitializer.cpp:19: error: `C::C(const C&)' is private
arrayinitializer.cpp:27: error: within this context
[jsalmon@river c++]$