【问题标题】:storing return values then using vs using directly c++存储返回值,然后使用 vs 直接使用 c++
【发布时间】:2010-09-23 22:32:28
【问题描述】:

大家好,我正在编写一些代码,但发现了一个奇怪的错误。函数 convert_vector2d(&i_scale) 将字符串转换为 vector2d(继承自 sf::vector2f)。如果您检查接下来的几行代码,我会执行两次相同的操作。

代码:全选

float x = convert_Vector2D(&i_scale).x;
float y = convert_Vector2D(&i_scale).y;
object.SetScale( ( convert_Vector2D(&i_scale) ) );
ss = object.GetScale();
object.SetScale( x , y );
ss = object.GetScale();

我第一次使用来自 convert_vector2d 和 ss = 1,1 的返回向量调用 setScale。然后我这次用 x, y(存储的结果)再次调用 object.setScale,当我调用 object.getScale 时,我得到 ss = 1,2(这是预期/正确的)。

我单步执行了转换函数,它通过两个函数调用返回 1,2。

代码:全选

const Vector2D Map::convert_Vector2D(std::string * string_to_convert)
{
    size_t foundit = 0;
    Vector2D temp;
    std::string one, two;
    if( (foundit = string_to_convert->find(',')) != std::string::npos &&
        string_to_convert->find_first_of(',') == string_to_convert->find_last_of(',') ) // only one comma per line.
    {
        one = string_to_convert->substr(0, foundit);
        two = string_to_convert->substr(foundit+1, string_to_convert->size()); // +1 to skip over the comma.

        temp.x = (float)strtod( one.c_str(), NULL );
        temp.y = (float)strtod( two.c_str(), NULL );

        check_conversion_errors_vector2d(temp, string_to_convert);
    }
    else
    {
        Debugger::print("MapLoader: Error: more then one comma on line %d of file %s. Stopping reading of file.\n",
            i_Current_Line, mMapName.c_str() );
        i_QuitParsing = true; // TODO: maybe add return after this line?
    }

    return temp;
}

对我为什么会出现不同的行为有任何想法吗?

void Drawable::SetScale(float ScaleX, float ScaleY)
{
    SetScaleX(ScaleX);
    SetScaleY(ScaleY);
}

void Drawable::SetScale(const Vector2f& Scale)
{
    SetScaleX(Scale.x);
    SetScaleY(Scale.y);
}

void Drawable::SetScaleX(float FactorX)
{
    if (FactorX > 0)
     {
          myScale.x       = FactorX;
          myNeedUpdate    = true;
          myInvNeedUpdate = true;
     }
}


void Drawable::SetScaleY(float FactorY)
{
     if (FactorY > 0)
     {
          myScale.y = FactorY;
          myNeedUpdate    = true;
          myInvNeedUpdate = true;
     }
 }

SFML 复制构造函数和成员变量:

// = equals operator assignment
Vector2D& operator=(const Vector2D &rhs)
{
    if(this == &rhs)
    {
        return *this;
    }
    else
    {
        this->x = rhs.x;
        this->y = rhs.y;
        return *this;
    }
}
// = equals operator assignment
Vector2D& operator=(const sf::Vector2f &rhs)
{
    this->x = rhs.x;
    this->y = rhs.y;
    return *this;
}

float x, y;

【问题讨论】:

  • SetScale() 这两个函数长什么样子?
  • 您能告诉我们Vector2fVector2D 的代码(特别是成员变量,以及您拥有的任何复制构造函数)吗?我怀疑您可能在两者中都声明了名为 xy 的成员。
  • 确实如此,x 和 y 都声明了。为什么这很重要?我将复制构造函数添加到vector2d的原始帖子中,sf::vector2f为:sfml-dev.org/documentation/1.5/Vector2_8hpp-source.htm
  • 我删除了 vector2D 中的 x, y 声明并且它起作用了,无论出于何种原因在 vector2f 和 vector2d 中声明 x,y 都搞砸了。有人愿意解释吗?

标签: c++ function parameters return-value


【解决方案1】:

不要在栈上分配 Vector2D,在堆上用 new 分配。您在函数之外对 temp 的引用未定义,可能是垃圾。

【讨论】:

  • 我正在返回值,通过返回我认为我不再引用存储在堆栈中的变量 temp 的值,因为它被返回了?
  • @Ben:您使用正确。我怀疑@user 误读了您的代码。 :)
  • 非常外交的乔纳森,我用 Java 编码太久了,我的大脑线被交叉了......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2013-10-28
  • 2021-08-07
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多