C++ string passed as reference

I’ve got the strangest problem with a C++ program I’m writing, and I’ve got no idea why it’s happening.

The situation:

  1. I’ve got a class where one of its methods accepts three strings as references and modifies two of the strings within the method.

The signature is:

bool fooclass::barmethod(const string& str1, string& str2, string& str3)

  1. 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.

What happens? Let’s say in main I write…

string galump(“blah”), woozle(“dummy”), mimsy(“saucer”);

[…]

temp.barmethod(galump, woozle, mimsy);

…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.

What’s going on here?

Do you have something like this:


int main(int argc, char ** argv)
{
//...
    string galump("blah"), woozle("dummy"), mimsy("saucer");

//...

    if(something) {
        string woozle, mimsy;

        // ...

        temp.barmethod(galump, woozle, mimsy);
    }

    // woozle and mimsy in this scope were not modified
}

Also, are these std::strings or are they some custom class? If they are a custom class are you sure that the class works?

They’re std::strings and there’s no conditional. It’s an extremely straightforward driver.

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).

Insufficient data.

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:

StringClass.h



#ifndef STRINGCLASS_H
#define STRINGCLASS_H
#include <string>
using namespace std;
class StringClass {
  public:
  StringClass();
  ~StringClass();
  void barmethod(const string& str1, string& str2, string& str3);
};
#endif


StringClass.C



#include "StringClass.h"
#include <iostream>
#include <string>
using namespace std;

StringClass::StringClass () {
}
StringClass::~StringClass() {
}
void StringClass::barmethod(const string& str1, string& str2, string& str3) {

  cout << "string1:  " << str1 << endl;
  cout << "string2:  " << str2 << endl;
  cout << "string3:  " << str3 << endl;

}


testString.C



#include <iostream>
#include <string>
#include "StringClass.h"
using namespace std;

int main(int argc, char **argv) {
  StringClass temp;
  string galump("blah"), woozle("dummy"), mimsy("saucer");
  temp.barmethod(galump, woozle, mimsy);
}


When you run testString you get the expected output:
string1: blah
string2: dummy
string3: saucer

Can you give us some code snippets to see how you’re handling the strings once you’re in foo::barmethod?

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.

I’ve solved it.

The constructor was also calling that method, and so I was seeing output from the constructor’s call rather than from the call from main.

Very obvious in hindsight. Thanks everyone for the help.

The *all *are! :slight_smile: