Template copy constructor question

Discussion in 'C++' started by Indrawati Yahya, Nov 12, 2004.

  1. 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!
    Indrawati Yahya, Nov 12, 2004
    #1
    1. Advertising

  2. Indrawati Yahya wrote in
    news: in comp.lang.c++:

    > Hi
    >
    > I am having trouble in defining a templated class' copy constructor.


    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 >.

    > Here is the simplest code that represent my problem:
    >
    > template<typename T1>
    > class Foo
    > {


    template < typename U > friend 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) {}
    >


    > };
    >
    > 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?
    >


    Use the friend declaration above.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Nov 12, 2004
    #2
    1. Advertising

  3. Rob Williscroft <> wrote in message news:<Xns959F5AB76A5FDukcoREMOVEfreenetrtw@130.133.1.4>...
    > Indrawati Yahya wrote in
    > news: in comp.lang.c++:
    >
    > > Hi
    > >
    > > I am having trouble in defining a templated class' copy constructor.

    >
    > 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 >.


    Thanks for pointing that out, as otherwise it may cause a fatal bug
    when the actual copy constructor is called.

    >
    > > Here is the simplest code that represent my problem:
    > >
    > > template<typename T1>
    > > class Foo
    > > {

    >
    > template < typename U > friend 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) {}
    > >

    >
    > > };
    > >
    > > 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?
    > >

    >
    > Use the friend declaration above.


    Doh! My VC++6 rejects the code. Comeau's accepted it fine though.
    Thanks!

    >
    > Rob.
    Indrawati Yahya, Nov 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    462
    Mike Wahler
    Jan 25, 2004
  2. ali
    Replies:
    4
    Views:
    568
    David Harmon
    Mar 5, 2007
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
  4. Replies:
    4
    Views:
    705
    peter koch
    Nov 30, 2008
  5. cinsk
    Replies:
    35
    Views:
    2,581
    James Kanze
    Oct 11, 2010
Loading...

Share This Page