How do you do assignment operator within a template ?

Discussion in 'C++' started by imutate, Oct 3, 2006.

  1. imutate

    imutate 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 ? }
    };
     
    imutate, Oct 3, 2006
    #1
    1. Advertisements

  2. imutate

    Boris Guest

    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. Advertisements

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

  4. somthing like this should work.
    T& operator=(const T& rhs) {
    {
    // this->thing = rhs.thing;
    ... etc
    return *this;
    }
     
    Gianni Mariani, Oct 3, 2006
    #4
  5. imutate

    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. 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
     
    Victor Bazarov, Oct 3, 2006
    #6
  7. imutate

    Greg Guest

    No, you can't do that. Indexing operator has only one argument.
    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. 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
     
    Victor Bazarov, Oct 3, 2006
    #8
  9. imutate

    Greg Guest

    The latter.
    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. Advertisements

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 (here). After that, you can post your question and our members will help you out.