Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy control in derived class

Reply
Thread Tools

copy control in derived class

 
 
campos
Guest
Posts: n/a
 
      01-08-2007
"Effective C++ 3rd Edition" Item 6, P39

-------------------------------------------------------

class Uncopyable {
protected: // allow construction
Uncopyable() {} // and destruction of
~Uncopyable() {} // derived objects...
private:
Uncopyable(const Uncopyable&); // ...but prevent copying
Uncopyable& operator=(const Uncopyable&);
};

To keep HomeForSale objects from being copied, all we have to do now is
inherit from Uncopyable:

class HomeForSale: private Uncopyable { // class no longer
... //
declares copy ctor or
}; // copy
assign. operator

-------------------------------------------------------

I understand that the copy ctor and copy assignment of class
HomeForSale will be implicitly generated by the compiler if needed. In
the copy ctor of HomeForSale, first it will call the default ctor of
Uncopyable implicitly. As for copy assignment, it won't call the
counterpart in the base class.


But why the author says:

-------------------------------------------------------
This works, because compilers will try to generate a copy constructor
and a copy assignment operator if anybody - even a member or friend
function - tries to copy a HomeForSale object. As Item 12 explains,
the compiler-generated versions of these functions will try to call
their base class counterparts, and those calls will be rejected,
because the copying operations are private in the base class.
-------------------------------------------------------

I tried it in VC7.0 and it did come out with a compiled error instead
of a link error.

Thanks in advance!

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-08-2007
"campos" <> wrote in message
news: oups.com...
> "Effective C++ 3rd Edition" Item 6, P39
>
> -------------------------------------------------------
>
> class Uncopyable {
> protected: // allow construction
> Uncopyable() {} // and destruction of
> ~Uncopyable() {} // derived objects...
> private:
> Uncopyable(const Uncopyable&); // ...but prevent copying
> Uncopyable& operator=(const Uncopyable&);
> };
>
> To keep HomeForSale objects from being copied, all we have to do now is
> inherit from Uncopyable:
>
> class HomeForSale: private Uncopyable { // class no longer
> ... //
> declares copy ctor or
> }; // copy
> assign. operator
>
> -------------------------------------------------------
>
> I understand that the copy ctor and copy assignment of class
> HomeForSale will be implicitly generated by the compiler if needed. In
> the copy ctor of HomeForSale, first it will call the default ctor of
> Uncopyable implicitly. As for copy assignment, it won't call the
> counterpart in the base class.
>
>
> But why the author says:
>
> -------------------------------------------------------
> This works, because compilers will try to generate a copy constructor
> and a copy assignment operator if anybody - even a member or friend
> function - tries to copy a HomeForSale object. As Item 12 explains,
> the compiler-generated versions of these functions will try to call
> their base class counterparts, and those calls will be rejected,
> because the copying operations are private in the base class.
> -------------------------------------------------------
>
> I tried it in VC7.0 and it did come out with a compiled error instead
> of a link error.
>
> Thanks in advance!


Right. So what's your question? That it came out with compile error
instead of link error? That's what happens. I don't see anything in what
you quoted saying it would generate a link error. What are you asking?


 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-08-2007

"campos" <> wrote in message
news: oups.com...
> "Effective C++ 3rd Edition" Item 6, P39
> -------------------------------------------------------
> This works, because compilers will try to generate a copy constructor
> and a copy assignment operator if anybody - even a member or friend
> function - tries to copy a HomeForSale object. As Item 12 explains,
> the compiler-generated versions of these functions will try to call
> their base class counterparts, and those calls will be rejected,
> because the copying operations are private in the base class.
> -------------------------------------------------------
>
> I tried it in VC7.0 and it did come out with a compiled error instead
> of a link error.
>
> Thanks in advance!
>


So? A compile error is just what you'd expect to get, right? The author says
nothing about link errors (you will get link errors when someone that's both
a friend of Base _and_ Derived (or is a member of one and a friend of the
other) tries to copy the object)

- Sylvester


 
Reply With Quote
 
campos
Guest
Posts: n/a
 
      01-08-2007
Thanks Sylvester,

What I am puzzled with is that the author says "the compiler-generated
versions of these functions will try to call their base class
counterparts, and those calls will be rejected,
because the copying operations are private in the base class."

I don't know why the counterparts in base class will be called.


Sylvester Hesp wrote:
> "campos" <> wrote in message
> news: oups.com...
> > "Effective C++ 3rd Edition" Item 6, P39
> > -------------------------------------------------------
> > This works, because compilers will try to generate a copy constructor
> > and a copy assignment operator if anybody - even a member or friend
> > function - tries to copy a HomeForSale object. As Item 12 explains,
> > the compiler-generated versions of these functions will try to call
> > their base class counterparts, and those calls will be rejected,
> > because the copying operations are private in the base class.
> > -------------------------------------------------------
> >
> > I tried it in VC7.0 and it did come out with a compiled error instead
> > of a link error.
> >
> > Thanks in advance!
> >

>
> So? A compile error is just what you'd expect to get, right? The author says
> nothing about link errors (you will get link errors when someone that's both
> a friend of Base _and_ Derived (or is a member of one and a friend of the
> other) tries to copy the object)
>
> - Sylvester


 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-08-2007
The generated copy ctor of the derived class will call the copy ctor of it's
base. Which makes sense, the derived class is only responsible for copying
it's own members, and any copying that needs to be done inside the base
class is the responsibility for the base class itself. So in your example,
the compiler will implicitely generate the following function:

HomeForSale(const HomeForSale & other) : Uncopyable(other)
{
}

But the copy ctor of Uncopyable is inaccessible for HomeForSale members (as
it's private to Uncopyable and HomeForSale isn't a friend of Uncopyable), so
you'll get a compile error.

Exactly the same goes for assignment operators.

- Sylvester

"campos" <> wrote in message
news: oups.com...
> Thanks Sylvester,
>
> What I am puzzled with is that the author says "the compiler-generated
> versions of these functions will try to call their base class
> counterparts, and those calls will be rejected,
> because the copying operations are private in the base class."
>
> I don't know why the counterparts in base class will be called.
>
>
> Sylvester Hesp wrote:
>> "campos" <> wrote in message
>> news: oups.com...
>> > "Effective C++ 3rd Edition" Item 6, P39
>> > -------------------------------------------------------
>> > This works, because compilers will try to generate a copy constructor
>> > and a copy assignment operator if anybody - even a member or friend
>> > function - tries to copy a HomeForSale object. As Item 12 explains,
>> > the compiler-generated versions of these functions will try to call
>> > their base class counterparts, and those calls will be rejected,
>> > because the copying operations are private in the base class.
>> > -------------------------------------------------------
>> >
>> > I tried it in VC7.0 and it did come out with a compiled error instead
>> > of a link error.
>> >
>> > Thanks in advance!
>> >

>>
>> So? A compile error is just what you'd expect to get, right? The author
>> says
>> nothing about link errors (you will get link errors when someone that's
>> both
>> a friend of Base _and_ Derived (or is a member of one and a friend of the
>> other) tries to copy the object)
>>
>> - Sylvester

>



 
Reply With Quote
 
campos
Guest
Posts: n/a
 
      01-08-2007
Oh, I see.

If I define a copy ctor explicitly in derived class without calling the
copy ctor of base class in initializer list, the compiler will call the
default ctor of base class implicitly.

But if I do not define the copy ctor in derived class, the compiler
will generate a copy ctor which will call the copy ctor of base class
at first.

That's the difference.


However, as for copy assignment, the compiler will implicitly generate
the following fuction:

HomeForSale& operator=(const HomeForSale& rhs)
{
Uncopyable:perator=(rhs);
}

The calling of function in base class is where the compile error comes
from.



Sylvester Hesp wrote:
> The generated copy ctor of the derived class will call the copy ctor of it's
> base. Which makes sense, the derived class is only responsible for copying
> it's own members, and any copying that needs to be done inside the base
> class is the responsibility for the base class itself. So in your example,
> the compiler will implicitely generate the following function:
>
> HomeForSale(const HomeForSale & other) : Uncopyable(other)
> {
> }
>
> But the copy ctor of Uncopyable is inaccessible for HomeForSale members (as
> it's private to Uncopyable and HomeForSale isn't a friend of Uncopyable), so
> you'll get a compile error.
>
> Exactly the same goes for assignment operators.
>
> - Sylvester
>
> "campos" <> wrote in message
> news: oups.com...
> > Thanks Sylvester,
> >
> > What I am puzzled with is that the author says "the compiler-generated
> > versions of these functions will try to call their base class
> > counterparts, and those calls will be rejected,
> > because the copying operations are private in the base class."
> >
> > I don't know why the counterparts in base class will be called.
> >
> >
> > Sylvester Hesp wrote:
> >> "campos" <> wrote in message
> >> news: oups.com...
> >> > "Effective C++ 3rd Edition" Item 6, P39
> >> > -------------------------------------------------------
> >> > This works, because compilers will try to generate a copy constructor
> >> > and a copy assignment operator if anybody - even a member or friend
> >> > function - tries to copy a HomeForSale object. As Item 12 explains,
> >> > the compiler-generated versions of these functions will try to call
> >> > their base class counterparts, and those calls will be rejected,
> >> > because the copying operations are private in the base class.
> >> > -------------------------------------------------------
> >> >
> >> > I tried it in VC7.0 and it did come out with a compiled error instead
> >> > of a link error.
> >> >
> >> > Thanks in advance!
> >> >
> >>
> >> So? A compile error is just what you'd expect to get, right? The author
> >> says
> >> nothing about link errors (you will get link errors when someone that's
> >> both
> >> a friend of Base _and_ Derived (or is a member of one and a friend of the
> >> other) tries to copy the object)
> >>
> >> - Sylvester

> >


 
Reply With Quote
 
tolgaceylanus@yahoo.com
Guest
Posts: n/a
 
      01-08-2007

Uncopyable can be fragile IMHO, if you happen to define a copy-cons,
and assignment
in the derived class by mistake, then uncopyable is pretty much ignored
since the custom
assignment and copy-cons do not call the base counterparts
automatically.

Probably this scenario would cause long hours debugging to figure out
what's going on...
You have some classes that inherit uncopyable, yet somewhere deep in
the code
some code is able to copy these...

Just something to keep in mind...

Tolga Ceylan

 
Reply With Quote
 
tolgaceylanus@yahoo.com
Guest
Posts: n/a
 
      01-08-2007

campos wrote:
> Oh, I see.
>
> If I define a copy ctor explicitly in derived class without calling the
> copy ctor of base class in initializer list, the compiler will call the
> default ctor of base class implicitly.


Not correct. Custom copy-ctor will not call base counterpart
automatically.
The same goes for assignment, custom assignment operator will not
call the base counter part automatically.

>
> But if I do not define the copy ctor in derived class, the compiler
> will generate a copy ctor which will call the copy ctor of base class
> at first.
>


Yes... Compiler generated copy-cons will call base counter part
automatically.

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-08-2007

"campos" <> wrote in message
news: oups.com...
> Oh, I see.
>
> If I define a copy ctor explicitly in derived class without calling the
> copy ctor of base class in initializer list, the compiler will call the
> default ctor of base class implicitly.


Ah right, I missed the part where you defined your own copy ctor in
HomeForSale. Well yes, if you omit the base copy ctor call in the
initialized list, the default ctor for Uncopyable will get called

- Sylvester


 
Reply With Quote
 
campos
Guest
Posts: n/a
 
      01-08-2007

wrote:
> campos wrote:
> > Oh, I see.
> >
> > If I define a copy ctor explicitly in derived class without calling the
> > copy ctor of base class in initializer list, the compiler will call the
> > default ctor of base class implicitly.

>
> Not correct. Custom copy-ctor will not call base counterpart
> automatically.
> The same goes for assignment, custom assignment operator will not
> call the base counter part automatically.



Hey, I've tried it just now.

Defaul ctor will be called at the beginning if default copy ctor of
base class is not declared explicitly in initializer list. If the
default ctor of base class is unavailable, a compile error will occur.


Anyway, thanks all~


>
> >
> > But if I do not define the copy ctor in derived class, the compiler
> > will generate a copy ctor which will call the copy ctor of base class
> > at first.
> >

>
> Yes... Compiler generated copy-cons will call base counter part
> automatically.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Re: Copy assignment for derived class when base class has privatemembers Karl Heinz Buchegger C++ 3 08-06-2003 11:44 AM
Re: Copy assignment for derived class when base class has private members John Harrison C++ 0 08-06-2003 08:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57