简体   繁体   中英

C++ Overriding assignment ( = ) operator of std::string

I really not mean overriding it cause I know it's not possible (unless I made my own). But I how do I do that in the way like this

strText = "bla bla";
strText.Compile();    //<--- I want this one to be implicitly call.

I know I can do that using a method like this

updateText(const std::string& text)
{
    strText = text;
    Compile();
}

or

std::string& updateText()
{
    Compile();      //hmm not sure about this. never try
    return strText;
}

But is there any other technique how can I achieve this implicitly by doing only

strText = newText;   //<--automatically call the `Compile()`

??

Please let me know before I give it up to updateText()

Thanks!

The only solution I can think of is to define my::string (store internally an std::string to give basic string functionality) and define:

my::string(const char*);

in order to allow implicit conversions from C style strings and my::string , then define:

my::string& operator=(const char*);

to implement the calling of your Compile function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM