Re: Templates and Copy Constructors. Again.
From: Matt Bitten (mbitten73_at_yahoo.com)
Date: 02/23/05
- Next message: Richard Cavell: "Re: using QueryPerformance"
- Previous message: Donovan Rebbechi: "Re: How to redefine operator delete/delete[] via macro?"
- In reply to: Victor Bazarov: "Re: Templates and Copy Constructors. Again."
- Next in thread: Victor Bazarov: "Re: Templates and Copy Constructors. Again."
- Reply: Victor Bazarov: "Re: Templates and Copy Constructors. Again."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 22 Feb 2005 16:35:54 -0800
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(const Foo & another)
{ std::cout << "copy constructor" << std::endl; }
template <typename T>
Foo<T> & Foo<T>::operator=(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.
- Next message: Richard Cavell: "Re: using QueryPerformance"
- Previous message: Donovan Rebbechi: "Re: How to redefine operator delete/delete[] via macro?"
- In reply to: Victor Bazarov: "Re: Templates and Copy Constructors. Again."
- Next in thread: Victor Bazarov: "Re: Templates and Copy Constructors. Again."
- Reply: Victor Bazarov: "Re: Templates and Copy Constructors. Again."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|