C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to Move data between two Templated classes

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
David Barto
Guest





PostPosted: Fri Apr 23, 2004 4:16 pm    Post subject: How to Move data between two Templated classes Reply with quote



Use address at the bottom of the post to reply.

I've got a problem with this one. I've got some data in a templated
class:

template<class T>
class bar {
private:
int a;
float b;
public:
other stuff.
};

What I want to do is transfer this information between 2 versions of this
class:
bar<BAZZ> x;
bar<TOUGH> y;

y = x; /* move a and b from y to x */

And I just can't figure it out. Nothing seems to work.

Using gcc 3.3, Linux

What am I missing?


--
David Barto [email]barto (AT) ucsd (DOT) edu[/email] [email]david.barto (AT) viasat (DOT) com[/email]
How can you call yourself a 'Computer Professional' if
you hook Windows computers up to the Internet?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Antoun Kanawati
Guest





PostPosted: Sat Apr 24, 2004 12:22 am    Post subject: Re: How to Move data between two Templated classes Reply with quote



David Barto wrote:
Quote:
Use address at the bottom of the post to reply.

I've got a problem with this one. I've got some data in a templated
class:

template<class T
class bar {
private:
int a;
float b;
public:
other stuff.
};

What I want to do is transfer this information between 2 versions of this
class:
bar bar<TOUGH> y;

y = x; /* move a and b from y to x */

And I just can't figure it out. Nothing seems to work.

Using gcc 3.3, Linux

What am I missing?

You can add a template operator=

template<class T>
class bar {
private:
int a;
float b;
public:
template<typename T2>
bar &operator=(const bar<T2> &that) { ... }
other stuff.
};

You're still left with the a and b being private, which
makes them inaccessible to the operator.

For this, the "other stuff" clause should include public
accessors for a and b.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Xenos
Guest





PostPosted: Sat Apr 24, 2004 12:34 am    Post subject: Re: How to Move data between two Templated classes Reply with quote




"David Barto" <dbarto (AT) viasat (DOT) com> wrote

Quote:
Use address at the bottom of the post to reply.

I've got a problem with this one. I've got some data in a templated
class:

template<class T
class bar {
private:
int a;
float b;
public:
other stuff.
};

What I want to do is transfer this information between 2 versions of this
class:
bar bar<TOUGH> y;

y = x; /* move a and b from y to x */

And I just can't figure it out. Nothing seems to work.

Using gcc 3.3, Linux

What am I missing?


--
David Barto [email]barto (AT) ucsd (DOT) edu[/email] [email]david.barto (AT) viasat (DOT) com[/email]
How can you call yourself a 'Computer Professional' if
you hook Windows computers up to the Internet?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


template<class T>
class bar {
template<class U>
const bar<T>& operator= (const bar<U>& t) const {a = t.a; b = t.b; /*
other stuff */ return *this;}
private:
int a;
float b;
public:
other stuff.
};






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
moswald
Guest





PostPosted: Sat Apr 24, 2004 12:36 am    Post subject: Re: How to Move data between two Templated classes Reply with quote

Add this code to your class:

template<typename _tyOther>
bar<T> &operator=(const bar<_tyOther> &rhs)
{
a = rhs.a;
b = rhs.b;
return *this;
}

Note, I find that some compilers have difficulty with this concept.
YMMV.

Also, if anyone can show me the proper syntax for the implementation of a
template member of a templated class (ie, if the method is declared in the
class, but then implemented as an inline function outside the class body),
I'd be most appreciative.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Hyman Rosen
Guest





PostPosted: Sat Apr 24, 2004 12:39 am    Post subject: Re: How to Move data between two Templated classes Reply with quote

David Barto wrote:
Quote:
What am I missing?

A templated assignment operator.

template <class T> class bar {
//...
template <class U> bar &operator=(const bar<U> &r)
{
a = r.a;
b = r.b;
// ...
return *this;
}
// ...
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Sat Apr 24, 2004 12:40 am    Post subject: Re: How to Move data between two Templated classes Reply with quote

David Barto <dbarto (AT) viasat (DOT) com> writes:

Quote:
Use address at the bottom of the post to reply.

I've got a problem with this one. I've got some data in a templated
class:

template<class T
class bar {
private:
int a;
float b;
public:

bar(){}

template bar(bar<other> const& b):a(b.a_()),b(b.b_()){}
template<class other>
bar& operator=(bar<other> const& b){return *this=bar(b);}

int a_()const{return a;}
float b_()const{return b;}

Quote:
};

What I want to do is transfer this information between 2 versions of this
class:
bar<BAZZ> x;
bar<TOUGH> y;

y = x; /* move a and b from y to x */
[snip]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Serge N
Guest





PostPosted: Sat Apr 24, 2004 12:50 am    Post subject: Re: How to Move data between two Templated classes Reply with quote


"David Barto" <dbarto (AT) viasat (DOT) com> wrote

Quote:
Use address at the bottom of the post to reply.

I've got a problem with this one. I've got some data in a templated
class:

template<class T
class bar {
private:
int a;
float b;
public:
// ============ try this: ===========

template bar& operator=(bar<U> const& other)
{ a = other.a; b = other.b; return *this; }
// ===============================

Quote:
other stuff.
};

HTH

Sergey




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephen C. Dewhurst
Guest





PostPosted: Sat Apr 24, 2004 12:53 am    Post subject: Re: How to Move data between two Templated classes Reply with quote

On 23 Apr 2004 12:16:26 -0400, David Barto <dbarto (AT) viasat (DOT) com> wrote:

Quote:
Use address at the bottom of the post to reply.

I've got a problem with this one. I've got some data in a templated
class:

template<class T
class bar {
private:
int a;
float b;
public:
other stuff.
};

What I want to do is transfer this information between 2 versions of this
class:
bar bar<TOUGH> y;

y = x; /* move a and b from y to x */

And I just can't figure it out. Nothing seems to work.

Using gcc 3.3, Linux

What am I missing?

You can try this:

template<class T>
class bar {
private:
int a;
float b;
template <class X> friend class bar;
public:
template <typename Other>
bar &operator =( const bar<Other> &that ) {
a = that.a;
b = that.b;
return *this;
}
};

However, that friend declaration fails on some otherwise respectable
compilers, so you might have to provide (yuck) accessors for a and b.

Steve
www.semantics.org

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Sat Apr 24, 2004 11:22 am    Post subject: Re: How to Move data between two Templated classes Reply with quote

"moswald" <moswald (AT) glasseye (DOT) net> writes:

Quote:
Add this code to your class:

template<typename _tyOther
bar {
a = rhs.a;
b = rhs.b;
return *this;
}

Note, I find that some compilers have difficulty with this concept.
YMMV.

Also, if anyone can show me the proper syntax for the implementation of a
template member of a templated class (ie, if the method is declared in the
class, but then implemented as an inline function outside the class body),
I'd be most appreciative.

template<class type>template<class other>
bar<type>& bar<type>::operator=(bar<other> const& b)
{return *this=bar(b);}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.