Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do you do assignment operator within a template ?

Reply
Thread Tools

How do you do assignment operator within a template ?

 
 
imutate@hotmail.co.uk
Guest
Posts: n/a
 
      10-03-2006
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 ? }
};

 
Reply With Quote
 
 
 
 
Boris
Guest
Posts: n/a
 
      10-03-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2006
(E-Mail Removed) 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


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      10-03-2006
(E-Mail Removed) 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;
}

> };
>

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-03-2006
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;
}

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2006
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


 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-03-2006
> 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);

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2006
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<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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-03-2006
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments