storing return values then using vs using directly

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

storing return values then using vs using directly

Post by short »

Hey guys, I was writing some code and I found a peculiar error. The function convert_vector2d(&i_scale) converts a string to a vector2d (inherits from sf::vector2f). If you examine the next few lines of code, I am doing the same operation twice.

Code: Select all

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();
First time I call setScale with the return vector from convert_vector2d and ss = 1,1. I then call object.setScale again this time with x, y (the stored results) and when I call object.getScale I get ss = 1,2 (which is expected/correct).

I stepped through the convert function and it returns 1,2 through both function calls.

Code: Select all

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

Any ideas on why I'm getting different behavior?

edit: I'm also having a conversation about it on stackoverflow: http://stackoverflow.com/questions/3782 ... directly-c
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: storing return values then using vs using directly

Post by short »

Okay, so by having Vector2D inherit from vector2f and defining member variables x,y in both caused an error. I removed the declaration of x,y in vector2d (inherited class) and it works perfectly. I guess the compiler wasn't sure which x,y was supposed to be updated. Does anyone know EXACTLY what happens if you declare x,y in a base class and an inherited class?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: storing return values then using vs using directly

Post by Scoody »

It hides the x,y in the parent class, so you'd have to use the scope resolution operator to access them. vector2f::x and vector2f::y.
Post Reply