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 {
I think I declare the copy cconstructor as follows:
Foo(const Foo & another);
And if I do not declare it that way, the compiler writes a
copy constructor for me. Is that right?
Now, if I understand correctly, "Foo" is not a type, but a
template. So what does it mean that this function I'm
declaring takes a const reference to a Foo, when Foo is not
a type? Is it that "Foo", inside the class definition, is
a synonym for the type of the current object?
If so, does that mean I can do this (inside the class
definition):
int bar(Foo x);
Can I declare the copy constructor differently? In
particular, are any of the following, written inside the
class definition, equivalent to the above:
Foo(const Foo<T> & another);
Foo<T>(const Foo & another);
Foo<T>(const Foo<T> & another);
Now say I want to implement my copy constructor outside the
body of the class definition. How do I write the definition
of the function?
Thanks for any help. I know that a reasonable answer to
some of my questions above is "Try it out, you ninny!"
However, I want to write standard-conforming code, and I am
worried that my compiler and The Standard might not agree
on some points. 3 1928
"Matt Bitten" <mb*******@yaho o.com> wrote... [...] Suppose I have a class template:
template <typename T> class Foo {
I think I declare the copy cconstructor as follows:
Foo(const Foo & another);
And if I do not declare it that way, the compiler writes a copy constructor for me. Is that right?
Yes.
Now, if I understand correctly, "Foo" is not a type, but a template. So what does it mean that this function I'm declaring takes a const reference to a Foo, when Foo is not a type? Is it that "Foo", inside the class definition, is a synonym for the type of the current object?
'Foo' outside the template is a template-id. Inside the template
definition, 'Foo' by itself is "the same type as the one being
instantiated". IOW, if you write Foo<int> somewhere, and then
make it so its copy c-tor is used, then the "real" copy c-tor will
have this signature:
Foo<int>::Foo<i nt>(const Foo<int>& otherfoo);
If so, does that mean I can do this (inside the class definition):
int bar(Foo x);
You absolutely can do this. This statement declares a function
'bar' that takes one argument of the same type as the instantiated
template and returns an int.
Can I declare the copy constructor differently? In particular, are any of the following, written inside the class definition, equivalent to the above:
Foo(const Foo<T> & another); Foo<T>(const Foo & another); Foo<T>(const Foo<T> & another);
IIRC, inside the definition of {template<typen ame T> class Foo}
the type-id "Foo<T>" means exactly the same thing as "Foo".
Now say I want to implement my copy constructor outside the body of the class definition. How do I write the definition of the function?
template<class T> Foo<T>::Foo(con st Foo& other)
{
... // whatever
} Thanks for any help. I know that a reasonable answer to some of my questions above is "Try it out, you ninny!" However, I want to write standard-conforming code, and I am worried that my compiler and The Standard might not agree on some points.
There can be two points of view on this. While you are supposed
to write standard code, you are still using a very particular
compiler and your code should also conform to that compiler's
idiosyncrasies.
V
Thanks for the help.
Now, what is it that is NOT a copy constructor, that has everyone so
confused? I read in the standard in 12.8 that a "template
constructor" is not a copy constructor. However, a global search of
the standard turns up no other use of the term "template
constructor". So what is a template constructor?
FWIW, I tried a few things out. I'm using VC++ 7.0. The following
works:
#include <iostream>
template <typename T>
class Foo {
public:
Foo();
Foo(const Foo & another);
Foo & operator=(const Foo & another);
~Foo();
};
template <typename T>
Foo<T>::Foo()
{ std::cout << "default constructor" << std::endl; }
template <typename T>
Foo<T>::Foo(con st Foo & another)
{ std::cout << "copy constructor" << std::endl; }
template <typename T>
Foo<T> & Foo<T>::operato r=(const Foo & another)
{ std::cout << "copy assignment" << std::endl; return *this; }
template <typename T>
Foo<T>::~Foo()
{ std::cout << "destructor " << std::endl; }
int main()
{
Foo<int> f1; // Try default constructor
Foo<int> f2(f1); // Try copy constructor
f2 = f1; // Try copy assignment
// Implicitly try destructor (twice)
}
And the printout shows that I am successfully overriding all four of
the automatically generated functions.
I can change ALL of the copies of "Foo" with no "<" after them into
"Foo<T>", except for the one after "class", and it still works.
I cannot change any of the "Foo<T>" above into just "Foo". So Foo as
a namespace or a return type, outside the class definition, must be
"Foo<T>". Foo as a parameter type for a member function or the name
of a constructor or destructor, is fine, as is any use of Foo inside
the class definition.
It is not clear to me that this is what the standard says, however.
"Matt Bitten" <mb*******@yaho o.com> wrote... Now, what is it that is NOT a copy constructor, that has everyone so
Everyone? I'd probably refrained from speaking for *everyone*...
confused? I read in the standard in 12.8 that a "template constructor" is not a copy constructor. However, a global search of the standard turns up no other use of the term "template constructor". So what is a template constructor? [...] It is not clear to me that this is what the standard says, however.
class Foo {
public:
template<class T> Foo(T const& t); // a template constructor
};
template<class T> class Bar {
public:
template<class U> Bar(const Bar<U>&); // a template constructor
};
The point is that in neither case the template constructor implements
or works in lieu of the compiler-generated copy constructor, although
you might think that if in the first case "T" is Foo, then the c-tor
becomes
Foo(Foo const& t);
and in the second case if U is the same as T, then it becomes
Bar<T>(const Bar<T>&);
The Standard says that it's not going to happen. In both classes Foo
and Bar<T>, the copy constructor that has a very specific signature
is going to be provided and will take over for all copy-construction
needs.
Victor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Edward Diener |
last post by:
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates
an object from a copy of another object of the same kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
by: Bo Xu |
last post by:
Object of Combination
By Bo Xu
Introduction
A combination of n things, taken s at a time, often referred as an
s-combination out of n, is a way to select a subset of size s from a
given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald
E. Knuth gives several methods (algorithms) to generate all the
s-combinations in . In such procedure-oriented way, each
s-combination is processed while it's being generated. In some
|
by: Bore Biko |
last post by:
Dear,
I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates...
|
by: Jon Slaughter |
last post by:
#pragma once
#include <vector>
class empty_class
{
};
template <int _I, int _J, class _element, class _property>
class RDES_T
{
|
by: tschwartz |
last post by:
I'm trying to write a stylesheet which removes nodes which are empty
as a result of other template processing.
For example, given the following xml, I'd like to:
- remove all "b" elements
- remove all "a" elements which, as a result of "b" element
removal, now have no children
so, starting with:
| |
by: abhishek.smu |
last post by:
Given an XML like:
<root>
<node>8</node>
<node>21</node>
<node>-7</node>
<node>13</node>
<node>43</node>
<node>2</node>
</root>
|
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,...
|
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...
|
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...
|
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...
|
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();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |