peter koch
11/30/2008 10:03:00 AM
On 30 Nov., 10:50, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 29, 1:15 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
>
>
>
>
>
> > On 29 Nov., 12:16, "ravajappa.go...@gmail.com"
> > <ravajappa.go...@gmail.com> wrote:
> > > Hi! Can somebody tell me when does copy constructor 1 or 2
> > > gets called in a template class below? And if you can
> > > explain the difference between the 1 and 2 below would be
> > > great?
> > > template<typename T, size_t size>
> > > class container {
> > > public:
> > > container() //default constructor.
> > > {
> > > }
> > > template<typename cT, size_t cs> // Copy
> > > constructor 1
> > > container(const container<cT, cs>& rhs)
> > > {
> > > ........
> > > }
> > > container(const container& rhs) // Copy
> > > constructor 2
> > > {
> > > ..........
> > > }
> > > private: ..... //
> > > private members defined here.
> > > }
> > The second constructor is the default copy-constructor and
> > will be called whenever a normal copy constructor would
> > normally be called.
>
> That's not quite true. The only real effect of the second
> (other than those associated with any constructor) is to
> suppress the automatic generation of the copy constructor by the
> compiler. In any given situation, which constructor is called
> will depend on operator overload resolution, and it is quite
> possible to design a class so that the template constructor is
> called for copying. (Admittedly, this is a rather exotic point,
> and it's not likely to affect most people's code.)
>
While I was aware of your first point, your second point is new to me.
Could you give me an example of how to write the copy-constructor in a
way that it will not be called to construct an object from an object
of the same type, using my generic templated constructor instead? I
have wanted to do so a few times but thought it was not possible and
instead ended up with two identical pieces of code.
/Peter