Operator commonalities

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Eisenberg

    Operator commonalities

    Hi!

    I have a class C with one type template parameter T and a member of
    type T. Class C has two assignment operators that take C's
    instantiated with the same and with another T, respectively. The
    implementations of these differ only in that the latter does not
    assign the T member. How do I best get to write the common part only
    once?

    Here's the situation for those who read code better than English. Of
    course, my real class and operators are bigger than that:

    template<class T>
    class C {
    public:
    C<T>& operator=(const C<T>& rhs)
    { t_ = rhs.t_; data_ = rhs.data_; return *this; }

    template<RhsT>
    C<T>& operator=(const C<RhsT>& rhs)
    { data_ = rhs.data_; return *this; }

    private:
    int data_;
    T t_;
    };


    Thank you,
    Martin
  • Rolf Magnus

    #2
    Re: Operator commonalities

    Martin Eisenberg wrote:
    [color=blue]
    > Hi!
    >
    > I have a class C with one type template parameter T and a member of
    > type T.[/color]

    You mean you have a class template called C.
    [color=blue]
    > Class C has two assignment operators that take C's
    > instantiated with the same and with another T, respectively. The
    > implementations of these differ only in that the latter does not
    > assign the T member. How do I best get to write the common part only
    > once?[/color]

    Why would you want to?

    Comment

    • Martin Eisenberg

      #3
      Re: Operator commonalities

      Rolf Magnus wrote:
      [color=blue]
      > Martin Eisenberg wrote:
      >[color=green]
      >> Hi!
      >>
      >> I have a class C with one type template parameter T and a
      >> member of type T.[/color]
      >
      > You mean you have a class template called C.
      >[color=green]
      >> Class C has two assignment operators that take C's
      >> instantiated with the same and with another T, respectively.
      >> The implementations of these differ only in that the latter
      >> does not assign the T member. How do I best get to write the
      >> common part only once?[/color]
      >
      > Why would you want to?[/color]

      Come to think of it, why would anyone actually want to avoid
      duplication? Seriously, I don't get your question.

      Comment

      • Victor Bazarov

        #4
        Re: Operator commonalities

        "Martin Eisenberg" <martin.eisenbe rgNOS@PAMudo.ed u> wrote...[color=blue]
        > Rolf Magnus wrote:
        >[color=green]
        > > Martin Eisenberg wrote:
        > >[color=darkred]
        > >> Hi!
        > >>
        > >> I have a class C with one type template parameter T and a
        > >> member of type T.[/color]
        > >
        > > You mean you have a class template called C.
        > >[color=darkred]
        > >> Class C has two assignment operators that take C's
        > >> instantiated with the same and with another T, respectively.
        > >> The implementations of these differ only in that the latter
        > >> does not assign the T member. How do I best get to write the
        > >> common part only once?[/color]
        > >
        > > Why would you want to?[/color]
        >
        > Come to think of it, why would anyone actually want to avoid
        > duplication? Seriously, I don't get your question.[/color]

        template<class T> class C {
        T t;
        void commonCode() { /*whatever*/ }
        public:
        C(T t) : t(t) {}
        C& operator =(C const& otherC) {
        commonCode();
        t = otherC.t;
        return *this;
        }

        template<class U> C& operator =(C<U> const& otherCU) {
        commonCode();
        return *this;
        }
        };

        int main() {
        C<int> ci(42);
        C<double> cd(3.1415926);
        ci = cd;
        }

        Perhaps I don't understand your problem. Do you think you could
        illustrate with C++?

        Victor


        Comment

        • David Fisher

          #5
          Re: Operator commonalities

          "Martin Eisenberg" <martin.eisenbe rgNOS@PAMudo.ed u> wrote:
          [color=blue]
          > The implementations of these differ only in that the latter does not
          > assign the T member. How do I best get to write the common part only
          > once?[/color]
          [color=blue]
          > template<class T>
          > class C {
          > public:[/color]
          template <> // <--------- added[color=blue]
          > C<T>& operator=(const C<T>& rhs)
          > { t_ = rhs.t_; data_ = rhs.data_; return *this; }
          >
          > template<typena me RhsT> // <-------- added "typename"
          > C<T>& operator=(const C<RhsT>& rhs)
          > { data_ = rhs.data_; return *this; }
          >
          > private:
          > int data_;
          > T t_;
          > };[/color]

          (Needed to add "template <>" to the first declaration, at least under Visual
          C++).

          This won't quite work anyway ... "data_ = rhs.data_" is illegal, since in
          the context of C<T>, C<RhsT>::_data is private

          David F


          Comment

          • Martin Eisenberg

            #6
            Re: Operator commonalities

            Victor Bazarov wrote:
            [color=blue]
            > Perhaps I don't understand your problem. Do you think you could
            > illustrate with C++?[/color]

            Duh... You've understood my "problem" perfectly well. Sometimes I
            think terminal blindness is but weeks away. Thanks anyway,


            Martin

            Comment

            Working...