From: Frank on
Dear people,

I have a class C1 which contains a member variable of class C2.
C2 didn't have a copy constructor up to now but due to a
change needed (and got) one, and an assignment operator
as well. Now when compiling, I get en error C2558:
"class C1: No copy constructor available or copy constructor
has been declared 'explicit'". It is correct that C1 doesn't
have a copy constructor but I thought that the default copy
constructor just calls the assignment operators of the
members. Since C2 has an explicitly programmed
assignment operator - where is the mistake?

TIA!
From: Joseph M. Newcomer on
Yes, you will need to supply a copy constructor to C2 if you expect C1 to be copyable. If
every component of C1 has a copy constructor, then C1 can be copied.

A copy constructor and an assignment operator are quite different from the viewpoint of
C++, even if a copy constructor calls the assignment operator. It's part of the deep
rules of C++ syntax.
joe

On Mon, 10 May 2010 08:05:00 -0700 (PDT), Frank <jerk(a)gmx.de> wrote:

>Dear people,
>
>I have a class C1 which contains a member variable of class C2.
>C2 didn't have a copy constructor up to now but due to a
>change needed (and got) one, and an assignment operator
>as well. Now when compiling, I get en error C2558:
>"class C1: No copy constructor available or copy constructor
>has been declared 'explicit'". It is correct that C1 doesn't
>have a copy constructor but I thought that the default copy
>constructor just calls the assignment operators of the
>members. Since C2 has an explicitly programmed
>assignment operator - where is the mistake?
>
>TIA!
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Doug Harrison [MVP] on
On Mon, 10 May 2010 08:05:00 -0700 (PDT), Frank <jerk(a)gmx.de> wrote:

>Dear people,
>
>I have a class C1 which contains a member variable of class C2.
>C2 didn't have a copy constructor up to now but due to a
>change needed (and got) one, and an assignment operator
>as well. Now when compiling, I get en error C2558:
>"class C1: No copy constructor available or copy constructor
>has been declared 'explicit'". It is correct that C1 doesn't
>have a copy constructor but I thought that the default copy
>constructor just calls the assignment operators of the
>members. Since C2 has an explicitly programmed
>assignment operator - where is the mistake?

Constructors create objects where none existed before, while assignment
operators overwrite existing objects. Therefore, the default copy ctor
invokes only copy ctors, and the default assignment operator invokes only
assignment operators. They do this for each member variable and base class.
So, for your scenario, if any copy ctor is inaccessible or cannot be
generated, the compiler will not be able to generate a copy ctor for C1.

--
Doug Harrison
Visual C++ MVP
From: Frank on
Doug Harrison wrote:

> Constructors create objects where none existed before, while assignment
> operators overwrite existing objects. Therefore, the default copy ctor
> invokes only copy ctors, and the default assignment operator invokes only
> assignment operators.

I see... so the copy constructor of C1 won't invoke
the assignment operators of its members to initialize
them like I thought?
From: Goran on
On May 10, 5:05 pm, Frank <j...(a)gmx.de> wrote:
> Dear people,
>
> I have a class C1 which contains a member variable of class C2.
> C2 didn't have a copy constructor up to now but due to a
> change needed (and got) one, and an assignment operator
> as well. Now when compiling, I get en error C2558:
> "class C1: No copy constructor available or copy constructor
> has been declared 'explicit'". It is correct that C1 doesn't
> have a copy constructor but I thought that the default copy
> constructor just calls the assignment operators of the
> members. Since C2 has an explicitly programmed
> assignment operator - where is the mistake?

Assignment != copy construction, hence:

class C1
{
C1(const C1& that);
C2 c2;
....
};

C1::C1(const C1& that) :
...,
c2(that.c2), // Missing copy ctor here.
...
{}

Also, keep "the rule of three" in mind:
http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

Goran.