By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

How do you do assignment operator within a template ?

P: n/a
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }
};

Oct 3 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
im*****@hotmail.co.uk wrote:
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }
};
What shall your assignment operator do? If you have a vector with 10 strings
and you assign a new string to your vector class shall it replace all the 10
strings?

Boris
Oct 3 '06 #2

P: n/a
im*****@hotmail.co.uk wrote:
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }
};
Not sure what it is you're asking. What do you need your operator
to do? Why are you assigning from an object to T to a vector? Do you
intend to assign all of the elements to that value? Then you need
a loop.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #3

P: n/a
im*****@hotmail.co.uk wrote:
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }

somthing like this should work.
T& operator=(const T& rhs) {
{
// this->thing = rhs.thing;
... etc
return *this;
}
};
Oct 3 '06 #4

P: n/a
oh sorry I did not think of that case, no I wanted to see when elements
are being assigned to

T& operator=[](const &i, const T& rhs) {
{
this->thing[i] = rhs.thing;
std::cout << "its here << " i << " " << rhs.thing;
return *this;
}

Oct 3 '06 #5

P: n/a
Greg wrote:
oh sorry I did not think of that case, no I wanted to see when
elements are being assigned to

T& operator=[](const &i, const T& rhs) {
{
this->thing[i] = rhs.thing;
std::cout << "its here << " i << " " << rhs.thing;
return *this;
}
No, you can't do that. Indexing operator has only one argument.

If you want to see when your elements are assigned to, do not provide
the indexing operator but instead have a 'set' type function:

void set(int i, const T& val);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #6

P: n/a
No, you can't do that. Indexing operator has only one argument.
>
If you want to see when your elements are assigned to, do not provide
the indexing operator but instead have a 'set' type function:

void set(int i, const T& val);
Will the assignment call this "set" e.g.

std::vector<intx;
x[0] = blah;

or would i have to replace "=" by set function ?
x.set(0, blah);

Oct 3 '06 #7

P: n/a
Greg wrote:
>No, you can't do that. Indexing operator has only one argument.

If you want to see when your elements are assigned to, do not provide
the indexing operator but instead have a 'set' type function:

void set(int i, const T& val);

Will the assignment call this "set" e.g.

std::vector<intx;
x[0] = blah;

or would i have to replace "=" by set function ?
x.set(0, blah);
The latter.

If you need to track assignment, you would have to implement a proxy
class. See my recent post to comp.lang.c++.moderated for an example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #8

P: n/a
The latter.
>
If you need to track assignment, you would have to implement a proxy
class. See my recent post to comp.lang.c++.moderated for an example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
OK thanks, I don't think this is the problem, I just realised a
potential flaw in my design something very different, I will post anew.

Oct 3 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.