开发者

Handling functions with more than one output parameters

开发者 https://www.devze.com 2023-02-24 07:40 出处:网络
How do we handle more than one output parameters in C++.I am beginner in C++ and currently i am trying to write a function A which calls another function B of some other class,Function B consists of 6

How do we handle more than one output parameters in C++.I am beginner in C++ and currently i am trying to write a function A which calls another function B of some other class,Function B consists of 6 parameters in total ,of which three are input parameters and the rest three are output parameters.How can i access all the three output parameters within my function A?I tried to do it in the following way...Can anyone help me to correct my code if i have gone wrong..?Please do 开发者_C百科help me friends..

class A ::functionA()
   {
      int in_a=1;
      string in_b= "name";
      int in_c=3; 
      int ot_a=0;
      int ot_b=0;
      string ot_s1="" 

      ClassB *classB();
      classB = classB.functionB(in_a,in_b,in_c,ot_a,ot_b,ot_s1); //is this way correct?
      ot_a= ? ;
      ot_b=? ;
      ot_s1=?
    }

can i use something like ot_a=classB.ot_a ?Please help me...


You have got the basic syntax of C++ wrong. ClassB *classB(); does not create any object, it declares a function prototype of function classB which returns ClassB*. To create a object you should do ClassB b; and then use b as you have done. The output variables will be correctly filled up by the function if it is taking its parameter by reference.


For multiple return values, you got generally two choices:

  • return a struct containing your return values
  • pass the return values in per reference.

Both examples demonstrated:

// first approach, struct return
struct myReturns{
  int int_return;
  float float_return;
};

myReturns MyFunc(int param1, char* param2, ...){
  // do some stuff with the parameters
  myReturns ret;
  ret.int_return = 42;
  ret.float_return = 13.37f;
  return ret;
}

// calling it:
myReturns ret = MyFunc(/*pass your parameters here*/);
int i = ret.int_return;
float f = ret.float_return;

// second approach, out parameters
void MyFunc(int inParam1, char* inParam2, int& outInt, float& outFloat){
  // do some stuff with the parameters
  outInt = 42;
  outFloat = 13.37f;
}

// calling it:
int i;
float f;
MyFunc(/*your parameters here*/,i,f);
// i and f are now changed with the return values


As mentionned in Xeo's answer, you can use return structures or references. There is another possibility, to use pointers. Pointers allows you to do one thing : if the function you call can be used to compute multiple informations, but you don't want all of them, you can pass NULL as the value of the pointer so that the function knows it doesn't need to fill these informations.

Of course, the function you call needs to be designed that way, it's not automatic.

void f()  
{   
    type1* p1 = new type1();  
    type2* p2 = NULL
    g(p1, p2);
}

void g(type1* param1, type2* param2)  
{
    //Do some computation here
    if (param1 != NULL)
    {
        //Do something here to fill param1
    }
    if (param2 != NULL)
    {
        //Do something here to fill param2
    }
}

But as a general rule, it's better to use references when you can, and pointers when tou have to. If the function doesn't handle the case when a pointer passed to it is NULL, you will end with a crash. References can't be NULL, so they avoid this problem.


Answer is: references.


ClassB *classB();
classB = classB.functionB(in_a,in_b,in_c,ot_a,ot_b,ot_s1);

By looking . operator after classB, I assume that you are thinking classB is an object. No, it is not.

ClassB *classB();

The above statement says - classB() is a function that takes no parameters and return type is a reference to ClassB.


If you can change functionB() then use pointers as parameters. This way you can change the value inside functionB() and they will be changed directly in functionA().

0

精彩评论

暂无评论...
验证码 换一张
取 消