C++在方法中传递常量字符串引用?

发布于 2024-08-12 08:35:34 字数 1080 浏览 3 评论 0原文

我试图初始化我的类的私有变量,将 const string &aString 作为参数传递给它。

这是我的方法:

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());
    }
}

我的问题如下,当我调用这个方法时,我这样做:

myInitializer.initWithTextureFile("myFile.bmp");

当我在 initWithTextureFile 的范围内时,name 变量的值为inTextureName。对于这个例子,如果我cout <<姓名<< endl;initWithTextureFile 内,我会得到 "myFile.bmp"

但是当我离开函数的范围时,name 会丢失它值,所以当我 cout <<姓名<< endl; 我在控制台中没有打印任何内容。

有人能指出我这里发生了什么吗?

名称已宣布:

private:
    std::string name;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

揽月 2024-08-19 08:35:34

如果您在类范围之外,并且 cout << name 完全编译,这意味着您有另一个名为 name 的变量,这就是正在被选取的变量。如果您想在课堂外引用它,您必须想出一种将其导出的方法。例如,您可能有一个类似 const std::string &GetName() { return 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; }.

旧梦荧光笔 2024-08-19 08:35:34

您要么在描述中省略了某些内容,要么没有显示可以帮助解决您的问题的适当代码。

这有效:

#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();
}

输出:

test

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
[浮城] 2024-08-19 08:35:34

那应该有效。您确定它没有在其他地方(例如 initImplementation 中)被修改吗?

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

慵挽 2024-08-19 08:35:34

问题可能与 name 变量有关:它是指向字符串的指针或引用而不是普通字符串吗?

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

海夕 2024-08-19 08:35:34

这里唯一合理的解释是您必须使用两个不同的 name 对象。当您退出该方法时,您声明为类成员的成员应该保留其值。只是在类方法之外,您必须打印一个完全不同的名称,它是空的。

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.

只想待在家 2024-08-19 08:35:34

我本想谈谈短暂的堆栈对象,但我意识到这是错误的。它可能与从 DLL 导出包含的类有关。

如果是这样,您可能会发现如下警告:

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'

此线程 描述了更多内容。

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.

浮光之海 2024-08-19 08:35:34

“名字”是如何宣布的?看起来它可能被声明为引用而不是对象。

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

罪歌 2024-08-19 08:35:34

尝试:

name = std::string(inTextureName);

Try:

name = std::string(inTextureName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文