Function not working as intended?

跟風遠走 提交于 2019-12-11 04:18:21

问题


First off I'd like to say I'm still a complete novice when it comes to Programming, and I've already searched for an answer to this problem, thing is, I'm not sure what's wrong.

I wrote a function to set parameters for an sf::Text object:

void FontParam(sf::Text obj, sf::Font font, std::string text, sf::Color color, int size, int positionX, int positionY)
{
    obj.setCharacterSize(size);
    obj.setFont(font);
    obj.setColor(color);
    obj.setString(text);
    obj.setPosition(positionX,positionY);
}

Using:

FontParam(t[0],font_Clubland,"R",sf::Color::White,100,100,100);

and drawing it will not work. Writing it as:

t[0].setFont(font_Clubland);
t[0].setColor(sf::Color::White);
t[0].setPosition(100,100);
t[0].setString("R");    
t[0].setCharacterSize(100);

and drawing it will.

Any help or tips are very much appreciated, thanks in advance!

EDIT: ---------------------------

Incase anyone was wondering, the access violation was happening because I wasn't referencing the sf::Font. The correct function would be:

void FontParam(sf::Text &obj, sf::Font &font, std::string text, sf::Color color, int size, int positionX, int positionY)
{
    obj.setCharacterSize(size);
    obj.setFont(font);
    obj.setColor(color);
    obj.setString(text);
    obj.setPosition(positionX,positionY);
}

Thanks for the help!


回答1:


You're passing a copy of t[0] into your function, changing the copy, and then discarding the changes. Instead, you probably want to pass your t[0] by reference so that the changes update into the original object. This is easily done by changing the parameter sf::Text obj to sf::Text& obj (Note the & added).




回答2:


The syntax

void FontParam(sf::Text obj

is called "pass by value" - you are passing the value of the original sf::Text to this function, and a new, local copy is created with a life-time of the function.

This is a default behavior because most of the time, you are passing values in to a function and do not want the function to modify your local work.

for (int i = 0; i < 10; ++i) {
    function(i);
}

if "function(i)" changed "i" to be 11, you'd get the wrong behavior.

To pass a variable more directly, so that the both the caller and the function share the same instance, you must use either pass-by-reference or pass-by-pointer.

Pass-by-reference uses a "&" decorator on the variable name at the receiver. Pass-by-pointer uses a "&" reference to take the address of the sending variable and a "*" as a decorator on the receiver, but you then also have to check for null pointers and use derefences, so I'm just going to describe pass-by-reference.

#include <iostream>
#include <string>

void foo1(std::string str) {
    str += "Hello";
}

void foo2(std::string str) {
    str += "World";
}

int main() {
    std::string str = "";
    foo1(str);
    foo2(str);
    std::cout << str << std::endl;
}

Will output "World".

You will probably have to retrofit your code with changes like this in a number of places.

A good way to help yourself avoid making mistakes like this is to decorate parameters as "const" when you specifically intend for them to be immutable, potentially forcing yourself to use local variables to capture modifications.

void print(const std::string& str, const int x, const int y)
{
    const int endX = x + widthPx(str) + 4;
    const int endY = y + heightPx(str) + 4;
    drawBow(x, y, endX, endY);
    const int cursX = x + 1;
    const int cursY = y + 1;
    cursor(cursX, cursY);
    drawText(str);
}

Note that when possible, you should take std::string by const& to avoid the overhead of copying (copying strings can get expensive quickly).



来源:https://stackoverflow.com/questions/18449265/function-not-working-as-intended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!