【发布时间】:2009-11-23 21:32:57
【问题描述】:
我正在尝试初始化我的类的私有变量,将 const 字符串 &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。对于这个例子,如果我在initWithTextureFile 中使用cout << name << endl;,我会得到"myFile.bmp"
但是当我离开函数的范围时,name 失去了它的价值,所以当我 cout << name << endl; 时,我没有在控制台中打印任何内容。
谁能告诉我这里发生了什么?
名称已声明:
private:
std::string name;
【问题讨论】:
-
没有足够的代码来回答这个问题。寻找隐藏在封闭范围内的局部“名称”变量(类成员、全局变量)。
-
查看名称的声明会有所帮助。另外,检查 initImplmentation() 是否有任何命名。
-
我刚刚添加了名称变量声明。
-
您是否在 Image 类的其他方法中打印? "name" 不应该在 Image 方法之外可见,因此 cout
-
请原谅这个愚蠢的问题:你在做“cout