Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'out' keyword in C++

Tags:

c++

Is there an equivalent to the C# 'out' keyword for out parameters but in C++?

I'd like to have a constructor also set a couple of references to variables where it is called.

like image 434
Dollarslice Avatar asked Sep 30 '11 07:09

Dollarslice


People also ask

What does the out keyword do?

You can use the out keyword in two contexts: As a parameter modifier, which lets you pass an argument to a method by reference rather than by value. In generic type parameter declarations for interfaces and delegates, which specifies that a type parameter is covariant.

What is out and ref keyword in C#?

ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.


1 Answers

No direct equivalent. In C++ you choose between passing by value, passing a pointer value, or passing a reference parameter. The latter two can be used to get data back from function.

VOID foo(int* pnA, int& nB)
{
  *pnA = 1;
  nB = 2;
}

int nA, nB;
foo(&nA, nB);
// nA == 1, nB == 2;
like image 193
Roman R. Avatar answered Oct 26 '22 03:10

Roman R.