简体   繁体   English

C ++在另一方法中使用方法的局部变量

[英]C++ Using a local variable of a method in another method

Does anyone know if there's a way to pass a variable that i have created inside of a method to another method . 有谁知道是否有办法将我在方法内部创建的变量传递给另一个方法。 I am posting part of the code so that it is clearer what i need to do 我正在发布代码的一部分,以便更清楚我需要做什么

int Point::trovaLato(Point *spigolo2, Point *sol_p, Point *pvet){
//code
//code
Point* lato=new Point(myvalue1,myvalue2,myvalue3);
return 0;
}

Now i want to use that variable 'lato' inside of 现在我想在里面使用变量“ lato”

int Rettangolo::interseca(Point *sol_p, Point *pvet){
int ritlat, test;
test = punti[0]->trovaLato(punti[1], sol_p, pvet); //this works
if(test){
    test = punti[1]->trovaLato(punti[2], sol_p, pvet);//ok
    if(!test){
        ritlat = lato->intersecaLato(punti[1], punti[2]); //doesn't know what lato is ofcourse :(
//more code
}

Thanks a lot 非常感谢

Change the return type to Point , and return one: 将返回类型更改为Point ,然后返回一个:

Point Point::trovaLato(Point* spigolo2, Point* sol_p, Point* pvet){
  //code
  //code
  return Point(myvalue1, myvalue2, myvalue3);
}

If you really need to return an int , you can return an std::pair<int, Point> . 如果确实需要返回一个int ,则可以返回一个std::pair<int, Point>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM