[vdr] Re: Problem with cChannel's copy constructor

Marcel Wiesweg marcel.wiesweg at gmx.de
Thu Aug 4 21:40:26 CEST 2005


> Hi,
>
> first thing, in your code, you should return *this in the operator= (and
> check if &a equals 'this'), which, I assume, you just
> forgot.
Yes, this code is just supposed to demonstrate the problem.
>
> 3:   second = pointer ? *pointer : A();
>
> @3: The compiler creates a anonymous, automatic object for class A on
> compile-time and copies members from *pointer via its copy-constructor
> into it (setting d=0!). Then, the operator= gets called for second with
> the temporary object as an argument (hence, copying d=0 into second).

Yes, running gdb made clear that something like this is happening, and I am 
willing to accept that this is according to the spec ;-)

>
> Try the attached code, which'll make it clearer, I presume...
>
> Apparently, the copy-constructor in cChannel needs explanation...

In sections.c:143, the code intends to copy the object, but the object is not 
copied, resulting in a bogus channel returned by cFilter::Channel(), and this 
is a bug in VDR.

I think that because of the possible implicit invocation of a copy constructor 
as seen in the example and in real code, a copy constructor should really be 
a copy constructor and equivalent to operator=. Otherwise you will run into 
bugs like above.

(See
http://braincore.blogspot.com/2005/06/koding-big-four.html
http://grammarian.homelinux.net/%7Empyne/weblog/tutorial/c%2B%2B-tidbits.html
for two KDE hackers' comments on providing copy constructors.)

Marcel



More information about the vdr mailing list