English 中文(简体)
C++ passing const string references in methods?
原标题:

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
",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);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签