I’ve got a calling function (actually, it’s main – I’m just testing the class) sending three strings and expecting the second and third arguments to be modified.
…the method is successfully called. However, the method receives the last two arguments as null strings, operates on those, returns, and then main carries on without its strings being modified by the method. It’s as if the method is operating on new strings that bear no relation to the strings being passed to it.
It has been a long time since I did C++ so I don’t remember the exact syntax, but as I recall the function call in main needs to indicate that it is sending the address of the string. I think the syntax is something like functioncall(&mimsy) where the & indicates “address of”. Then in the function definitition you should have string *str1, string *str2, string *str3… The * indicates that it is a reference to a variable. I remember having this same problem when I programmed in C and it was usually because I didn’t match up what I was sending (the address) with what I was receiving (the pointer).
I don’t see anything inherently wrong in the syntax you use in the OP. To prove this, I just wrote a quick test code and don’t see the behavior you describe. Here’s the code – maybe you can see what we’re doing differently:
As you describe it, it should work. What’s the code of the called function? Have you stepped through the debugger to ensure that the string objects are correctly initialized at the site of the call? If your main() function is as simple as you suggest, it’s unlikely that you’ve accidentally done something to the strings in between constructing them and calling the function, so I’d expect that the problem is most likely somewhere in barmethod().
No, in C++ it is preferable to avoid pointers and use references instead (with the & syntax in the declaration). You’d do it this way in C, but in C++ references have the same basic semantics as pointers (shared aliases to the same object) but are far preferable because they cannot be initialized with invalid values or have the address futzed with after initialization.