tags:

views:

258

answers:

7

Hello, 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;
A: 

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

siukurnin
just added the declaration of name.
Mr.Gando
A: 

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

Moishe
What difference would it make? The above assignment should copy the value to whatever that reference is attached to.
AndreyT
Maybe my C++ is too rusty. But if you do: "std::string name = inTextureName;" and then inTextureName goes out of scope, won't name be pointing at something random on the stack?
Moishe
AndreyT
A: 

Try:

name = std::string(inTextureName);
Skirwan
That didin't work...
Mr.Gando
The take a look at initImplementation and verify that it's not overwriting name. The code you posted *should* work; I've seen weirdness like you describe with some compilers/libraries that have a buggy reference-counted string implementation, but usually an explicit instantiation fixes that.
Skirwan
+1  A: 

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

progrmr
+2  A: 

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
stefanB
+3  A: 

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; }.

David Thornley
@MrGando indicated that he is accessing name outside of the the function scope, not outside the class scope.
Phillip Ngan
A: 

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.

AndreyT