Share via

class initialization with another class AND adding constant chars to class with overloading + operator

josh BAUER 166 Reputation points
2020-11-29T19:43:23.143+00:00
class myString {
public:
    myString(int number) {}
    myString(myString& paramMyString) {}
    myString(myString&& paramMyString) {}
    friend myString operator+(const char stString, myString& ndString);
    friend myString operator+(myString& stString, const char* ndString);
    friend myString operator+(myString& stString, myString& ndString);
    myString& operator=(myString& rhs)
    {
        return *this;
    }
    myString& operator=(myString&& rhs)
    {
        return *this;
    }
};
myString operator+(const char* stString, myString& ndString)
{
    myString rr(0);
    return rr;
}
myString operator+(myString& stString, const char* ndString)
{
    myString rr(0);
    return rr;
}
myString operator+(myString& stString, myString& ndString)
{
    myString rr(0);
    return rr;
}

Above is example of class. Then I would like to add string to it or initialize this class with same class like below:

myString kk();
        myString pp = kk;
        myString pp = "sth"+kk;

In second line just above I am getting error that cannot convert from myString to myString but
I made both copying constructor and moving constructor so I don't understand why it is not letting
me initialize myString with myString. How to this?

I would like like also add "sth" to myString. I made overloading operators so I should have not any error.
What is going wrong here?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2020-11-29T19:50:27.65+00:00

Kk is currently a function.

Try this fix: ‘myString kk;’ and add the parameterless constructor. Or maybe write ‘myString kk(0);’.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.