0

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 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...

3
  • What happened when you tried this? Did it work, or did it give you an error message? Commented Apr 14, 2011 at 6:32
  • please show the signature for B::functionB, or is what you are wondering about how to write functionB? Commented Apr 14, 2011 at 6:32
  • if this is homework, please add the homework tag to it. Commented Apr 14, 2011 at 6:36

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

@Victor: I excluded pointer since the OP doesn't seem to be very experienced in C++. :) And shoo, globals!
1

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.

Comments

0

Answer is: references.

Comments

0
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.

4 Comments

@Als - I didn't mention anything on those lines in my post.
@Als - No where I have given my code. It is just the OP's code in the question and I am explaining what it is actually meant.
@Als - What more can I say to the OP than what he/she is creating is not an object. Any how will consider your suggestion here after. Thanks.
My bad seems I have been pre occupied or i misread someonelses response as yours...Apologies..Keep up the good work.
0

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.