473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

static array initialization and private copy ctor


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++]$
Dec 8 '06 #1
3 3361
John Salmon wrote:
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
Dec 8 '06 #2
John Salmon wrote:
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).
I think its a good question. Here is simplified code. When you run
it, the copy constructor never prints out, indicating it is never
called, but when you make the copy constructor private, the code
doesn't compile. Any one else out there know why g++ is complaining
about a private copy constructor even when it appears never to call it?

#include <iostream>

struct C{
int i;

C(int i_) : i(i_)
{
}

C(const C& from) : i(from.i)
{
std::cout << "IT IS USED" << std::endl;
}

};

int main(int argc, char** argv)
{
C a = 1;
return 0;
}

----
Ivan
http://www.0x4849.net

Dec 9 '06 #3
Ivan Novick wrote:
John Salmon wrote:
>[..]
[..] Here is simplified code. When you run
it, the copy constructor never prints out, indicating it is never
called, but when you make the copy constructor private, the code
doesn't compile. Any one else out there know why g++ is complaining
about a private copy constructor even when it appears never to call
it?
The short answer is "because C++ Standard requires so". Any compiler
that wants to be considered compliant would do that, g++ included.
Not calling an available copy constructor is called optimisation which
may or may not occur (and is also explicitly allowed in the Standard).
However, the initialisation syntax in the original message is called
copy-initialisation, and it requires an *accessible* copy constructor.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: roger | last post by:
Here's a weird one... The code below works just fine when I build in DEBUG mode. Today, I tried to build my solution in RELEASE mode, and immediately fell over this problem - apparently...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
2
by: Mark P | last post by:
class A { private: explicit A( int i ) {} A( const A& ); public: static const A arr; };
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
3
by: josh | last post by:
Hi, if I've: class Date { public: Date(int m, int d, int y); }
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
16
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; }
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.