C++在方法中传递常量字符串引用?
我试图初始化我的类的私有变量,将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您在类范围之外,并且 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 namedname
, 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 likeconst 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:
Output:
那应该有效。您确定它没有在其他地方(例如 initImplementation 中)被修改吗?
That should work. Are you sure it is not being modified somewhere else, such as in initImplementation?
问题可能与 name 变量有关:它是指向字符串的指针或引用而不是普通字符串吗?
The problem probably have to do with the name variable : is it a pointer or ref to string instead of a plain string ?
这里唯一合理的解释是您必须使用两个不同的
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 differentname
, which is empty.我本想谈谈短暂的堆栈对象,但我意识到这是错误的。它可能与从 DLL 导出包含的类有关。
如果是这样,您可能会发现如下警告:
此线程 描述了更多内容。
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:
This thread describes more.
“名字”是如何宣布的?看起来它可能被声明为引用而不是对象。
How is 'name' declared? It seems like maybe it's declared as a reference instead of an object.
尝试:
Try: