operator= accepting a template?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Langston

    #1

    operator= accepting a template?

    What I want to do is have an operator= accept a template variable.

    I will have some classes which all will contain an instance of a different
    class. I want an operator= in yet a 3rd class to accept these classes and
    use the instance. This is confusing as heck, so here's kinda what I want to
    do:

    class COffsetMap
    {
    public:
    int Value;
    };

    class TestClass
    {
    TestClass& operator=( /* Here is where I want to accept a template */
    SomeVar )
    {
    SomeVar.FieldMa p // This is what I need to access
    }
    };

    class CCharFieldMap: public COffsetMap
    {
    public:
    void SetMap() { Value = 10; }
    };

    class CCharacter
    {
    public:
    CCharFieldMap FieldMap;
    };

    How would I set up the template, if I even can? The whole purpose in this
    is to do this in code:

    TestClass Instance;
    CCharacter Character;
    Character.Field Map.SetMap();

    Instance = Character;

    My SetMap actually sets a lot of values, which are actually offsets into the
    CCharacter class (variable locations). Saying Instance = Character I would
    like to load a vector in TestClass with the variables in Character which I
    can get to knowing the base address of Character and the offsets (Value = 10
    is just a bogus example right now).

    Is this possible? To have an operator=() accept a template parameter? And
    if so, how woudl I set up the template?


  • Thomas Tutone

    #2
    Re: operator= accepting a template?

    Jim Langston wrote:
    What I want to do is have an operator= accept a template variable.
    >
    I will have some classes which all will contain an instance of a different
    class. I want an operator= in yet a 3rd class to accept these classes and
    use the instance. This is confusing as heck, so here's kinda what I want to
    do:
    >
    class COffsetMap
    {
    public:
    int Value;
    };
    >
    class TestClass
    {
    TestClass& operator=( /* Here is where I want to accept a template */
    SomeVar )
    {
    SomeVar.FieldMa p // This is what I need to access
    }
    };
    template<typena me T>
    TestClass& operator=(const T& t)
    {
    // do whatever it is you do to t here
    return *this;
    }

    [snip]
    Is this possible?
    Yes, if I understand you properly.
    To have an operator=() accept a template parameter?
    Yes, that's quite ordinary - look at how any decent smart pointer
    template class is implemented.
    And
    if so, how woudl I set up the template?
    As above.

    Best regards,

    Tom

    Comment

    • Marcus Kwok

      #3
      Re: operator= accepting a template?

      Thomas Tutone <Thomas8675309@ yahoo.comwrote:
      template<typena me T>
      TestClass& operator=(const T& t)
      {
      // do whatever it is you do to t here
      return *this;
      }
      So, I was intrigued by this question and did a little experiment. I
      created a class and gave it a templated assignment operator. However,
      it seems the compiler-generated assignment operator is still present.
      Why does it call this one instead of the templated version? I was under
      the impression that it would not generate the compiler-generated version
      in the presence of a user-defined one. Does that only apply to the
      compiler-generated constructor?


      #include <iostream>
      #include <ostream>

      class Empty {};


      class Test {
      int i;
      double d;

      public:
      Test() : i(0), d(0.0) { }
      Test(int i_, double d_) : i(i_), d(d_) { }

      template <typename T>
      Test& operator=(const T& t);

      friend std::ostream& operator<<(std: :ostream& o, const Test& t);
      };

      template <typename T>
      Test& Test::operator= (const T& t)
      {
      return *this;
      }

      template <>
      Test& Test::operator= <int>(const int& i_)
      {
      i = i_;
      return *this;
      }

      template <>
      Test& Test::operator= <double>(cons t double& d_)
      {
      d = d_;
      return *this;
      }


      std::ostream& operator<<(std: :ostream& o, const Test& t)
      {
      return o << '(' << t.i << ", " << t.d << ')';
      }


      int main()
      {
      Test t;
      // outputs (0, 0) as expected
      std::cout << t << '\n';

      t = 3;
      // outputs (3, 0) as expected
      std::cout << t << '\n';

      t = 3.4;
      // outputs (3, 3.4) as expected
      std::cout << t << '\n';

      Empty e;
      t = e;
      // outputs (3, 3.4) as expected, since the unspecialized operator=()
      // is essentially a no-op
      std::cout << t << '\n';

      Test t2(42, 3.14);
      t = t2;
      // outputs (42, 3.14)
      // but I expected operator=<Test> () to be called,
      // hence (3, 3.4) as before
      std::cout << t << '\n';
      }

      --
      Marcus Kwok
      Replace 'invalid' with 'net' to reply

      Comment

      • Victor Bazarov

        #4
        Re: operator= accepting a template?

        Marcus Kwok wrote:
        Thomas Tutone <Thomas8675309@ yahoo.comwrote:
        > template<typena me T>
        > TestClass& operator=(const T& t)
        > {
        > // do whatever it is you do to t here
        > return *this;
        > }
        >
        So, I was intrigued by this question and did a little experiment. I
        created a class and gave it a templated assignment operator. However,
        it seems the compiler-generated assignment operator is still present.
        Why does it call this one instead of the templated version? I was
        under the impression that it would not generate the
        compiler-generated version in the presence of a user-defined one.
        Does that only apply to the compiler-generated constructor?
        A non-template version always wins. Implicitly declared/defined copy
        assingment op and copy-constructors are never replaced with template
        ones. That's how the Standard requires it (12.8/3).

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


        Comment

        • Howard

          #5
          Re: operator= accepting a template?


          "Victor Bazarov" <v.Abazarov@com Acast.comwrote in message
          news:eciehi$ipo $1@news.datemas .de...
          Marcus Kwok wrote:
          >Thomas Tutone <Thomas8675309@ yahoo.comwrote:
          >> template<typena me T>
          >> TestClass& operator=(const T& t)
          >> {
          >> // do whatever it is you do to t here
          >> return *this;
          >> }
          >>
          >So, I was intrigued by this question and did a little experiment. I
          >created a class and gave it a templated assignment operator. However,
          >it seems the compiler-generated assignment operator is still present.
          >Why does it call this one instead of the templated version? I was
          >under the impression that it would not generate the
          >compiler-generated version in the presence of a user-defined one.
          >Does that only apply to the compiler-generated constructor?
          >
          A non-template version always wins. Implicitly declared/defined copy
          assingment op and copy-constructors are never replaced with template
          ones. That's how the Standard requires it (12.8/3).
          >
          So, if I wanted a version of the assignment operator where the parameter was
          the same class (e.g., const Test&, using Marcus' example for his class
          Test), then I'd need to need to explicitly declare that operator in the Test
          class, instead of using a template, right?

          -Howard



          Comment

          • Victor Bazarov

            #6
            Re: operator= accepting a template?

            Howard wrote:
            "Victor Bazarov" <v.Abazarov@com Acast.comwrote in message
            news:eciehi$ipo $1@news.datemas .de...
            >Marcus Kwok wrote:
            >>Thomas Tutone <Thomas8675309@ yahoo.comwrote:
            >>> template<typena me T>
            >>> TestClass& operator=(const T& t)
            >>> {
            >>> // do whatever it is you do to t here
            >>> return *this;
            >>> }
            >>>
            >>So, I was intrigued by this question and did a little experiment. I
            >>created a class and gave it a templated assignment operator. However, it
            >>seems the compiler-generated assignment operator is
            >>still present. Why does it call this one instead of the templated
            >>version? I was under the impression that it would not generate the
            >>compiler-generated version in the presence of a user-defined one.
            >>Does that only apply to the compiler-generated constructor?
            >>
            >A non-template version always wins. Implicitly declared/defined copy
            >assingment op and copy-constructors are never replaced with template
            >ones. That's how the Standard requires it (12.8/3).
            >>
            >
            So, if I wanted a version of the assignment operator where the
            parameter was the same class (e.g., const Test&, using Marcus'
            example for his class Test), then I'd need to need to explicitly
            declare that operator in the Test class, instead of using a template,
            right?
            I believe so.

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


            Comment

            • Thomas Tutone

              #7
              Re: operator= accepting a template?


              Howard wrote:
              "Victor Bazarov" <v.Abazarov@com Acast.comwrote in message
              news:eciehi$ipo $1@news.datemas .de...
              Marcus Kwok wrote:
              Thomas Tutone <Thomas8675309@ yahoo.comwrote:
              > template<typena me T>
              > TestClass& operator=(const T& t)
              > {
              > // do whatever it is you do to t here
              > return *this;
              > }
              >
              So, I was intrigued by this question and did a little experiment. I
              created a class and gave it a templated assignment operator. However,
              it seems the compiler-generated assignment operator is still present.
              Why does it call this one instead of the templated version? I was
              under the impression that it would not generate the
              compiler-generated version in the presence of a user-defined one.
              Does that only apply to the compiler-generated constructor?
              A non-template version always wins. Implicitly declared/defined copy
              assingment op and copy-constructors are never replaced with template
              ones. That's how the Standard requires it (12.8/3).
              >
              So, if I wanted a version of the assignment operator where the parameter was
              the same class (e.g., const Test&, using Marcus' example for his class
              Test), then I'd need to need to explicitly declare that operator in the Test
              class, instead of using a template, right?
              You would need to explicitly define it, unless the implicitly
              declared/defined version met your needs. In that latter case, there
              would be little reason to explicitly define it (and no reason to
              explicitly declare it without defining it as well).

              Best regards,

              Tom

              Comment

              • Jim Langston

                #8
                Re: operator= accepting a template?


                "Thomas Tutone" <Thomas8675309@ yahoo.comwrote in message
                news:1156358016 .938934.306490@ i42g2000cwa.goo glegroups.com.. .
                Jim Langston wrote:
                >
                >What I want to do is have an operator= accept a template variable.
                >>
                >I will have some classes which all will contain an instance of a
                >different
                >class. I want an operator= in yet a 3rd class to accept these classes
                >and
                >use the instance. This is confusing as heck, so here's kinda what I want
                >to
                >do:
                >>
                >class COffsetMap
                >{
                >public:
                > int Value;
                >};
                >>
                >class TestClass
                >{
                > TestClass& operator=( /* Here is where I want to accept a template */
                >SomeVar )
                > {
                > SomeVar.FieldMa p // This is what I need to access
                > }
                >};
                >
                template<typena me T>
                TestClass& operator=(const T& t)
                {
                // do whatever it is you do to t here
                return *this;
                }
                >
                [snip]
                Thank you. Works as advertised.


                Comment

                Working...