Przeładowanie operatora *.

0

Cześć, zrobiłem taka klasę, która mnożny wektor przez skalar.

 #include <iostream>

using namespace std;

class wektor
{
public:
    int x;
    int y;
    int z;

    wektor(int xd=0, int yd=0, int zd=0): x(xd), y(yd), z(zd) {}

    wektor operator* (int liczba)
    {
        wektor temp;

        temp.x=x*liczba;
        temp.y=y*liczba;
        temp.z=z*liczba;

        return temp;
    }
};

main()
{
    wektor a(1,2,3),
           b(5,5,5),
           c;

    c=a*2;
    cout<<c.x<<" "<<c.y<<" "<<c.z<<endl;
}

A gdybym chciał zapisać taką wersję ?

a*2;

Wiem jak to zrobić ale czy można mieć dwie takie możliwości na raz?

void operator* (int liczba)
    {
        x*=liczba;
        y*=liczba;
        z*=liczba;
    } 
0

Wg mnie int to dziwne ograniczenie jak dla wektora.

wektor &operator*=(int liczba)
  {
   x*=liczba;
   y*=liczba;
   z*=liczba;
   return *this;
  }
wektor operator*(int liczba)const
  {
   wektor tmp(*this);
   tmp*=liczba;
   return tmp;
  }
friend wektor &operator*=(int liczba,const wektor &w)
  {
   return w*liczba;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1