0

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!

mr5
  • 3,438
  • 3
  • 40
  • 57
  • Half the code is missing. Is `updateText` a member function of some class? – Marc Claesen May 28 '13 at 09:47
  • @MarcClaesen it belongs to the same class. Not really important to discuss. – mr5 May 28 '13 at 09:51
  • @Morwenn can you give an example code? I think its interesting – mr5 May 28 '13 at 09:53
  • 2
    Do not derive a custom class from `std::string`. `std` library classes are not designed to be derived from. – Yakk - Adam Nevraumont May 28 '13 at 10:01
  • 1
    @Morwenn [Don't derive from `std::string`](http://stackoverflow.com/questions/6006860/why-should-one-not-derive-from-c-std-string-class), it does not have a virtual destructor. Favor composition over inheritance here, as generally. – Cody Gray - on strike May 28 '13 at 10:03
  • @Morwenn have you just deleted your answer? I was going to mark it as answer to my question but its not here. However I came up into another problem doing that so. – mr5 May 29 '13 at 07:45
  • @mr5 That was just an example, but as @Jeffrey had stated, deriving from `std::string` is not a good idea. Prefere using a `std::string` as a member variable. Moreover, it will be easier to use :) – Morwenn May 29 '13 at 07:51
  • @Morwenn sorry for being importunate but, can I still, somewhat overload the `std::string`'s = operator by doing that? – mr5 May 29 '13 at 08:05
  • No: the good solution is Jeffrey's one. That is, overlaoding your class `operator=`. You never overload `std::string`'s `operator=`, only your class one, but that does not prevent you from doing what you want to do. Search for tutorials on the net like "C++ - implement my own string class", there should be plenty of material that will help you learn what you need :) – Morwenn May 29 '13 at 08:13
  • @Morwenn please give me a link on how to make my own string. Thanks! – mr5 May 29 '13 at 08:30

1 Answers1

1

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.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I'm confused. Can you elaborate more about the code? I think that will do it. – mr5 May 28 '13 at 09:55
  • I see what you did there :) – mr5 May 28 '13 at 10:02
  • @mr5, you should post comments about my answer under my answer in order to avoid Morwenn to be notified. – Shoe May 28 '13 at 10:15
  • @Jeffrey I'm sorry for that. I'm still new about operator overloading in C++ so I can't try out what you are trying to say here. But I think Morwenn's answer would do it, and I think it's the same as what you are saying here. (Currently working on it) – mr5 May 28 '13 at 10:28
  • @Jeffrey Can your answer do this thing; `text = "some string";` and impilicitly `Compile()` will be call? If so, can I have an example code? – mr5 May 29 '13 at 08:02
  • @mr5, yup, as I said you have to define `my::string& operator=(const char*);` and inside his definition call `Compile()`: http://pastebin.com/xmKV2sF1. – Shoe May 29 '13 at 09:53
  • @Jeffrey I didn't see the example link. That's why it took me a while before I figure it out. Many thanks! and also for Morwenn for inhibiting me not to override `std::string`'s assignment operator. But I still didn't know how bad it is. – mr5 May 30 '13 at 08:52
  • @Jeffrey Now I want to hide the internal string in that implementation, is there anyway I can do to not redefine member functions of `std::string` again? like for example the `std::string::length()` will I need to redefine it as `my::length() { return _internalStr.length(); }` – mr5 May 30 '13 at 10:09
  • @mr5, no there's no way with this method. – Shoe May 30 '13 at 10:45