Connecting Tech Pros Worldwide Forums | Help | Site Map

Template copy constructor question

Indrawati Yahya
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi

I am having trouble in defining a templated class' copy constructor.
Here is the simplest code that represent my problem:

template<typename T1>
class Foo
{
public:
Foo(T1 bar = T1()): _bar(bar), _cached(false) {}
template<typename T2>Foo(const Foo<T2>& foo): _bar(T1(foo._bar)),
_cached(false) {}

//other operations...

private:
T1 _bar;
bool _cached;
};

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?

Thanks!

Rob Williscroft
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Template copy constructor question


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/
Indrawati Yahya
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Template copy constructor question


Rob Williscroft <rtw@freenet.co.uk> wrote in message news:<Xns959F5AB76A5FDukcoREMOVEfreenetrtw@130.133 .1.4>...[color=blue]
> Indrawati Yahya wrote in
> news:951f3f8d.0411120029.5d780a1d@posting.google.c om in comp.lang.c++:
>[color=green]
> > 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]

Thanks for pointing that out, as otherwise it may cause a fatal bug
when the actual copy constructor is called.
[color=blue]
>[color=green]
> > Here is the simplest code that represent my problem:
> >
> > template<typename T1>
> > class Foo
> > {[/color]
>
> template < typename U > friend class Foo;
>[color=green]
> > 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=green]
> > };
> >
> > 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.[/color]

Doh! My VC++6 rejects the code. Comeau's accepted it fine though.
Thanks!
[color=blue]
>
> Rob.[/color]
Closed Thread