Indrawati Yahya wrote in
news:951f3f8d.0411120029.5d780a1d@posting.google.c om in comp.lang.c++:
[color=blue]
> Hi
>
> I am having trouble in defining a templated class' copy constructor.[/color]
Minor nit this *isn't* a/the "copy constructor" its a converting
constructor, the compiler will still generate the copy constructor
and us it for copying a Foo< T > to another Foo< T >.
[color=blue]
> Here is the simplest code that represent my problem:
>
> template<typename T1>
> class Foo
> {[/color]
template < typename U > friend class Foo;
[color=blue]
> public:
> Foo(T1 bar = T1()): _bar(bar), _cached(false) {}
> template<typename T2>Foo(const Foo<T2>& foo): _bar(T1(foo._bar)),
> _cached(false) {}
>[/color]
[color=blue]
> };
>
> int main()
> {
> Foo<int> fooInt;
> Foo<double> fooDouble(fooInt);
> }
>
> The problem is, this code will not compile, since Foo<double> cannot
> access Foo<int> private member(_bar). Is there an elegant way to get
> around this, other than providing getters/setters for each members to
> be copied?
>[/color]
Use the friend declaration above.
Rob.
--
http://www.victim-prime.dsl.pipex.com/