Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c++ do you have 'out parameters' like in c#?

Tags:

c++

In c++ do you have 'out parameters' like in c#?

In c# the method signature would be:

bool TryGetValue(int key, out OrderType order)

The idea is the variable may not be assigned before passed but MUST be assigned before exiting the method.

MSDN out params link: http://msdn.microsoft.com/en-us/library/aa645764(v=vs.71).aspx

like image 975
CodingHero Avatar asked Dec 07 '22 16:12

CodingHero


2 Answers

There is nothing as strict as C# out parameters in C++. You can use pointers and references to pass values back but there is no guarantee by the compiler that they are assigned to within the function. They are much closer to C# ref than out

// Compiles just fine in C++
bool TryGetValue(int key, OrderType& order) {
  return false;
}
like image 196
JaredPar Avatar answered Dec 25 '22 16:12

JaredPar


No, there are no out parameters in C++ that force you to assign to it before exiting the function. Pointers and references are more like ref parameters in C#.

like image 36
Matti Virkkunen Avatar answered Dec 25 '22 16:12

Matti Virkkunen