Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template copy constructor question.

Reply
Thread Tools

Template copy constructor question.

 
 
ravajappa.gouda@gmail.com
Guest
Posts: n/a
 
      11-29-2008
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.
};
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      11-29-2008
On 29 Nov., 12:16, "(E-Mail Removed)"
<(E-Mail Removed)> 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.
The first one will be called in the other cases - that is if cT is
different from T or cs is different from size.
You do need the second constructor if it behaves differently from what
the compiler would generate: the templated copy constructor will never
be a substitute for the default constructor and thus you need to
supply it.

/Peter
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-29-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Can somebody tell me when does copy constructor 1 or 2 gets called
> in a template class below?


This question is already incorrect by itself. There's only one copy
constructor in your class. It is constructor 2. Template 1 does not
define any copy constructors. It can be used to generate _conversion_
constructors, but not copy constructors.

> And if you can explain the difference
> between the 1 and 2 below would be great?


I don't really see what needs to be explained here. These constructors
take arguments of different types. So the right one will be chosen
depending on the type of the argument used during construction. That's
basically it.

> 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.
> };


--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-30-2008
On Nov 29, 1:15*pm, peter koch <(E-Mail Removed)> wrote:
> On 29 Nov., 12:16, "(E-Mail Removed)"


> <(E-Mail Removed)> 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.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      11-30-2008
On 30 Nov., 10:50, James Kanze <(E-Mail Removed)> wrote:
> On Nov 29, 1:15*pm, peter koch <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On 29 Nov., 12:16, "(E-Mail Removed)"
> > <(E-Mail Removed)> 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
 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 4 11-15-2008 01:22 PM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 2 11-14-2008 10:04 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments