473,799 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10277
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.********@com Acast.net> wrote in message
news:Sh******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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.********@com Acast.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
1811
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 assignment, should copy constructor and assignment operator be unnecessary? 2. If a base class have a pointer member, should its derived classes also need copy constructor and assignment operator, no matter if a derived class itself has a...
3
1928
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 still feeling confused. Suppose I have a class template: template <typename T> class Foo {
11
3438
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
10
2636
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 section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
13
5025
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 to make it more manageable. Below is a draft of a document that I plan to give to my introductory C++ class. Please note that I have purposely left out any mention of safety issues in the ctors which could be resolved thru some combination...
2
6251
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 manipulation. Now A has a copy constructor and assignment operator. What will happen if copying goes on in B? Do I need to have an overloaded set of functions which call the same copy functions that A has available? Or are they virtual in the...
22
3629
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 v);
13
3973
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 is much appreciated. JD
9
2904
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 Foo() is included, i believe. };
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.