Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i cant make the asterisk operator overloading it does nothing on the code below it should repeat my string 5 times but it doesnot

.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 cant make the asterisk operator overloading it does nothing on the code below it should repeat my string 5 times but it doesnot

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading