I
Indrawati Yahya
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!
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!