How do you do assignment operator within a template ?

Discussion in 'C++' started by imutate@hotmail.co.uk, Oct 3, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Boris Guest

    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
     
    Boris, Oct 3, 2006
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Oct 3, 2006
    #3
  4. 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;
    }

    > };
    >
     
    Gianni Mariani, Oct 3, 2006
    #4
  5. Greg Guest

    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;
    }
     
    Greg, Oct 3, 2006
    #5
  6. 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 = 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
     
    Victor Bazarov, Oct 3, 2006
    #6
  7. Greg Guest

    > 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);
     
    Greg, Oct 3, 2006
    #7
  8. 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
     
    Victor Bazarov, Oct 3, 2006
    #8
  9. Greg Guest

    > 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.
     
    Greg, Oct 3, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    500
    Barry Schwarz
    Feb 8, 2005
  2. Martin Vorbrodt
    Replies:
    3
    Views:
    513
    Victor Bazarov
    Sep 9, 2005
  3. Replies:
    4
    Views:
    372
    Victor Bazarov
    Nov 9, 2005
  4. Chris
    Replies:
    34
    Views:
    1,553
  5. Hicham Mouline
    Replies:
    2
    Views:
    710
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page