Home > Software engineering >  I cant make the asterisk operator overloading it does nothing on the code below it should repeat my
I cant make the asterisk operator overloading it does nothing on the code below it should repeat my

Time:07-12

.h file

class Mystring
{
    friend std::ostream &operator<<(std::ostream &os, const Mystring &rhs);
    friend std::istream &operator>>(std::istream &in, Mystring &rhs);

private:enter code here
    char *str;      // pointer to a char[] that holds a C-style string
}

.cpp file

Mystring Mystring::operator * (int n) const {

   size_t buff_size = std::strlen(str) *n   1;
    char *buff = new char[buff_size];
    std::strcpy(buff,"");
    for (int i =1; i <=n; i  )
        std::strcat(buff,str);
    Mystring temp{buff}; 
    delete [] buff;
    return temp;
};


main{

  Mystring s3{"abcdef"};  
    s3*5;
    cout << s3 << endl; 
}

I can't make the asterisk operator overloading it does nothing on the code below it should repeat my string 5 times but it doesn't

CodePudding user response:

Your operator* will repeat the string, but you throw away the result of s3*5.

Try cout << s3*5 << endl;.

  • Related