template and inheritance

Discussion in 'C++' started by Olive, Jun 20, 2008.

  1. Olive

    Olive Guest

    Hello,

    Can you help me on this...
    Is there a way to fix the problem at the end of the main?
    If the two extra scalar product operator* were defined for scalar types
    only, it would be perfect. Can this be possible?

    I manage to merge the two first operator* using an is_base_of
    type_traits tricks, but I could not fix the friend operator*. And that
    was an ugly solution anyway.

    Thanks
    Olive

    struct vect {
    int v[3];

    //dot product
    int operator *(const vect& t) const {
    int res = 0;
    for (int i = 0; i < 3; ++i)
    res += v * t.v;
    return res;
    }
    //scalar product
    template <typename Y>
    vect operator *(const Y& t) const {
    vect res;
    for (int i = 0; i < 3; ++i)
    res.v += v * t;
    return res;
    }
    //scalar product in reverse order (t is the scalar value)
    template <typename Y>
    friend vect operator *(const Y& t, const vect& v) {
    return v*t;
    }
    };

    struct dummy {
    };

    int main() {
    {
    vect a;
    vect b;
    int dot = a*b; // fine
    vect c = a*2; // fine
    vect d = 2*a; // fine
    }
    {
    dummy a;
    dummy b;
    int dot = a*b; // the 3 operator* match
    dummy c = a*2; // the 3 operator* match
    dummy d = 2*a; // the 3 operator* match
    }

    return 0;
    }
     
    Olive, Jun 20, 2008
    #1
    1. Advertising

  2. Olive

    Fei Liu Guest

    Olive wrote:
    > Hello,
    >
    > Can you help me on this...
    > Is there a way to fix the problem at the end of the main?
    > If the two extra scalar product operator* were defined for scalar types
    > only, it would be perfect. Can this be possible?
    >
    > I manage to merge the two first operator* using an is_base_of
    > type_traits tricks, but I could not fix the friend operator*. And that
    > was an ugly solution anyway.
    >
    > Thanks
    > Olive
    >
    > struct vect {
    > int v[3];
    >
    > //dot product
    > int operator *(const vect& t) const {
    > int res = 0;
    > for (int i = 0; i < 3; ++i)
    > res += v * t.v;
    > return res;
    > }
    > //scalar product
    > template <typename Y>
    > vect operator *(const Y& t) const {
    > vect res;
    > for (int i = 0; i < 3; ++i)
    > res.v += v * t;
    > return res;
    > }
    > //scalar product in reverse order (t is the scalar value)
    > template <typename Y>
    > friend vect operator *(const Y& t, const vect& v) {
    > return v*t;
    > }
    > };
    >
    > struct dummy {
    > };
    >
    > int main() {
    > {
    > vect a;
    > vect b;
    > int dot = a*b; // fine
    > vect c = a*2; // fine
    > vect d = 2*a; // fine
    > }
    > {
    > dummy a;
    > dummy b;
    > int dot = a*b; // the 3 operator* match
    > dummy c = a*2; // the 3 operator* match
    > dummy d = 2*a; // the 3 operator* match
    > }
    >
    > return 0;
    > }

    You can use non class member function templates to achieve the desirable
    effects.

    Fei
     
    Fei Liu, Jun 20, 2008
    #2
    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. maxw_cc
    Replies:
    1
    Views:
    3,148
    Martijn van Steenbergen
    Dec 21, 2003
  2. Chris Theis
    Replies:
    2
    Views:
    480
    Chris Theis
    Jul 24, 2003
  3. tom_usenet
    Replies:
    0
    Views:
    539
    tom_usenet
    Jul 24, 2003
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,041
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    604
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page