| View previous topic :: View next topic |
| Author |
Message |
David Barto Guest
|
Posted: Fri Apr 23, 2004 4:16 pm Post subject: How to Move data between two Templated classes |
|
|
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
|
Posted: Sat Apr 24, 2004 12:22 am Post subject: Re: How to Move data between two Templated classes |
|
|
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
|
Posted: Sat Apr 24, 2004 12:34 am Post subject: Re: How to Move data between two Templated classes |
|
|
"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
|
Posted: Sat Apr 24, 2004 12:36 am Post subject: Re: How to Move data between two Templated classes |
|
|
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
|
Posted: Sat Apr 24, 2004 12:39 am Post subject: Re: How to Move data between two Templated classes |
|
|
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
|
Posted: Sat Apr 24, 2004 12:40 am Post subject: Re: How to Move data between two Templated classes |
|
|
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
|
Posted: Sat Apr 24, 2004 12:50 am Post subject: Re: How to Move data between two Templated classes |
|
|
"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; }
// ===============================
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
|
Posted: Sat Apr 24, 2004 12:53 am Post subject: Re: How to Move data between two Templated classes |
|
|
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
|
Posted: Sat Apr 24, 2004 11:22 am Post subject: Re: How to Move data between two Templated classes |
|
|
"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 |
|
 |
|