M
Matt Bitten
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.
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.