473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

copy constructor, assignment operator in template

In "C++ Templates, The Complete Guide" i read that template copy-con is
never default copy constructor, and template assignment-op is never a copy
assignment operator. Could someone please explain how I could
declate/override the two.

Thanx
Sep 9 '05 #1
3 10230
Martin Vorbrodt wrote:
In "C++ Templates, The Complete Guide" i read that template copy-con is
never default copy constructor, and template assignment-op is never a copy
assignment operator. Could someone please explain how I could
declate/override the two.


I am not sure how much you did understand, so, the text is talking about
this situation

struct A {
template<class T> A(T const&); // "copy"-constructor
};

This "copy" constructor will not be used if 'T' is 'A'. The compiler will
still generate another copy constructor,

A(A const&);

according to its usual rules of generating one, and will use it when copy
construction is performed from another 'A' object.

Same with the assignment op.

To make sure you _do_ have control over the copy construction process from
another 'A' object, define both the template and the non-template c-tors:

struct A {
template<class T> A(T const&);
A(A const&);
};

and do your "true" copy-construction in the latter and "pseudo" copy
construction in the former. Remember that there is no way to invoke one
c-tor from another, so if you need to do some common processing, pull it
out into a separate function.

V
Sep 9 '05 #2
what about this:

template <typename T>
class C {
public:
C(const T& t);
C(const C& c); // DO I STILL NEED THIS IF T = C ?
};

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Sh*******************@newsread1.mlpsca01.us.t o.verio.net...
Martin Vorbrodt wrote:
In "C++ Templates, The Complete Guide" i read that template copy-con is
never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain how I could
declate/override the two.


I am not sure how much you did understand, so, the text is talking about
this situation

struct A {
template<class T> A(T const&); // "copy"-constructor
};

This "copy" constructor will not be used if 'T' is 'A'. The compiler will
still generate another copy constructor,

A(A const&);

according to its usual rules of generating one, and will use it when copy
construction is performed from another 'A' object.

Same with the assignment op.

To make sure you _do_ have control over the copy construction process from
another 'A' object, define both the template and the non-template c-tors:

struct A {
template<class T> A(T const&);
A(A const&);
};

and do your "true" copy-construction in the latter and "pseudo" copy
construction in the former. Remember that there is no way to invoke one
c-tor from another, so if you need to do some common processing, pull it
out into a separate function.

V

Sep 9 '05 #3
Martin Vorbrodt wrote:
what about this:

template <typename T>
class C {
public:
C(const T& t);
C(const C& c); // DO I STILL NEED THIS IF T = C ?
Huh? T is C?
};

(a) Don't top-post.

(b) How can you have a template whose argument is itself?
"Victor Bazarov" <v.********@comAcast.net> wrote
[...]


V
Sep 9 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
3
by: Matt Bitten | last post by:
Hi, all. I have the same old problem about templates and copy constructors. I know this has been addressed hundreds of times, but despite perusing many old postings, and The Standard as well, I'm...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
2
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.