How do you do assignment operator within a template ?

I

imutate

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

Boris

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
 
V

Victor Bazarov

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
 
G

Gianni Mariani

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

Greg

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 = rhs.thing;
std::cout << "its here << " i << " " << rhs.thing;
return *this;
}
 
V

Victor Bazarov

Greg said:
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 = 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
 
G

Greg

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<int> x;
x[0] = blah;

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

Victor Bazarov

Greg said:
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<int> x;
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
 
G

Greg

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top