开发者

C++ passing const string references in methods?

开发者 https://www.devze.com 2022-12-12 02:15 出处:网络
I\'m trying to initialize a private variable of my Class passing a const string &aString to it as parameter.

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)
    {
    开发者_JAVA百科    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;


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


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


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


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


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.


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.


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


Try:

name = std::string(inTextureName);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号