Tutorial :C++ passing const string references in methods?



Question:

I'm trying to initialize a private variable of my Class passing a const string &aString to it as parameter.

Here's my method:

void Image::initWithTextureFile(const std::string &inTextureName)  {         Texture2D *imTexture = TEXTURE_MANAGER->createTexture(inTextureName);        if(imTexture)      {          texture = imTexture;          scale = 1.0f;          name = inTextureName; //name is a private string variable inside my class          initImplementation();      }else {          printf("Could not load texture when creating Image from file %s\n",inTextureName.c_str());      }  }  

My problem is the following, when I call this method I do it like:

myInitializer.initWithTextureFile("myFile.bmp");  

When I'm inside the scope of initWithTextureFile the name variable takes the value of inTextureName. For this example if I cout << name << endl; inside initWithTextureFile i would get "myFile.bmp"

But when I leave the scope of the function, name looses it's value, so when i cout << name << endl; I get nothing printed in the console.

Could anyone point me out to what's going on here?

Name is declared:

private:      std::string name;  


Solution:1

If you're outside the class scope, and cout << name compiles at all, it means you have another variable named name, and that's what's being picked up. If you want to refer to it outside the class, you'll have to come up with a way that will export it. You might, for example, have a member function like const std::string &GetName() { return name; }.


Solution:2

You either omitting something in your description or are not showing appropriate code that could help solve your problem.

This works:

#include <iostream>  #include <string>    using namespace std;    struct A  {      void f(const string& str) { name = str; }      void g() { cout << name << endl; }        string name;  };    int main()  {      A a;      a.f("test");      a.g();  }  

Output:

test  


Solution:3

That should work. Are you sure it is not being modified somewhere else, such as in initImplementation?


Solution:4

The problem probably have to do with the name variable : is it a pointer or ref to string instead of a plain string ?


Solution:5

The only reasonable explanation here is that you must be working with two different name objects. The one you declared as a class member should hold its value when you exit the method. It is just that outside the class method you must be printing a completely different name, which is empty.


Solution:6

I was going to say something about short-lived stack objects but I realised that was wrong. What it could be is something to do with exporting the containing class from a DLL.

If so, you might find a warning like this:

c:\yourclass.h(7): warning C4251: 'YourClass::name_' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'YourClass'  

This thread describes more.


Solution:7

How is 'name' declared? It seems like maybe it's declared as a reference instead of an object.


Solution:8

Try:

name = std::string(inTextureName);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »