Jak zdefiniowac operator+ ?

0

nie potrafie zdefiniowac operatora + w takim przypadku:

#ifndef __text_h__       
#define __text_h__
using namespace std;
class Text
{
    private:
        char *text;
        int lenght, temp;
    public:
        Text();
        Text(const Text &);
        Text(const char *);
        Text & operator=(const Text&);
        const Text & operator+(const Text&);
        char* wypisz();
        friend ostream & operator<<(ostream &, const Text &);
        ~Text();
};
#endif

wie ktos jak taki operator zdefiniowac??

0

? Zależy trochę od tego jak są inne operatory zaimplementowane
np. tak:

Text operator+(const Text& str2) const
{
  char* tmp = new char[str2.length + length + 1]; //zakładam że length to dlugość samego stringa bez null-byte na końcu
  strcpy(tmp,text);
  strcat(tmp,str2.text);
  Text retval(tmp);
  delete[] tmp; //zakładam ze konstruktor Text kopiuje sobie ten napis który dostanie jako argument, inaczej tego delete ma nie być
  return retval;
}

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