473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ref and out parameters

Hy!

I'd like to know how can I use ref and out parameters in c++.
I'd like to send (from C#) to my C++.net function a variable by reference,
so I could change it in my c++.net function.

And all I find about this topic is about C#.

thanks
- Samo
Nov 17 '05 #1
3 5698
SamoK wrote:
Hy!

I'd like to know how can I use ref and out parameters in c++.
I'd like to send (from C#) to my C++.net function a variable by reference,
so I could change it in my c++.net function.

And all I find about this topic is about C#.


Managed C++ doesn't provide those keywords, which are C# features, not .NET
features. AFAIK, it isn't possible to write a C++ function that C# views as
having out parameters, but you can write a C++ function that C# views as
having ref parameters. For example:

// a1.cpp

#using <mscorlib.dll>
using namespace System;

public __gc struct X
{
int x;

X()
: x(2)
{
}

void f1(int __gc * x)
{
++*x;
}

static void f2(X __gc * __gc * p)
{
X* q = new X;
q->x = (*p)->x;
++q->x;
*p = q;
}
};

// a.cs

using System;

public class MyClass
{
static void Main()
{
X x = new X();
X x2 = x;
int i = 0;
Console.WriteLine("i = {0}", i);
x.f1(ref i);
Console.WriteLine("i = {0}", i);
Console.WriteLine("x RefEquals x2 == {0}",
Object.ReferenceEquals(x, x2));
Console.WriteLine("x.x = {0}", x.x);
Console.WriteLine("x2.x = {0}", x2.x);
X.f2(ref x);
Console.WriteLine("x RefEquals x2 == {0}",
Object.ReferenceEquals(x, x2));
Console.WriteLine("x.x = {0}", x.x);
Console.WriteLine("x2.x = {0}", x2.x);
}
}

C>cl -LD -clr -W3 a1.cpp
C>csc a.cs /r:a1.dll
C>a
i = 0
i = 1
x RefEquals x2 == True
x.x = 2
x2.x = 2
x RefEquals x2 == False
x.x = 3
x2.x = 2

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
Hi Doug,

Managed C++ doesn't provide those keywords, which are C# features, not ..NET features. AFAIK, it isn't possible to write a C++ function that C# views as having out parameters,


Actually, it is perfectly possible. The only difference between a ref and an
out argument in C# is the presence of an out attribute on the generated
metadata, which can be easily generated by applying the
System::Runtime::InteropServices::OutAttribute attribute to the parameter in
the method's signature.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #3
Tomas Restrepo (MVP) wrote:
Actually, it is perfectly possible. The only difference between a ref and an
out argument in C# is the presence of an out attribute on the generated
metadata, which can be easily generated by applying the
System::Runtime::InteropServices::OutAttribute attribute to the parameter in
the method's signature.


Right you are. Before posting I looked at a C# exe in ildasm and it showed
these functions:

public void f1(ref int x)
{
}

public void f2(out int x)
{
x = 2;
}

as:

f1 : void(int32&)
f2 : void(int32&)

Opening the functions in disassembly windows gave:

.method public hidebysig instance void f1(int32& x) cil managed
.method public hidebysig instance void f2([out] int32& x) cil managed

There's that [out] attribute. :)

So, my C++ example code could be modified as:

using namespace System::Runtime::InteropServices;
....
void f1([Out] int __gc * x)
{
*x = 2;
}

Then the C# side would recognize and respect the out parameter. However, the
C++ side would not provide C#-like [out] semantics for C++ code; it would
merely emit the attribute metadata.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
2
by: Mark | last post by:
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one case I created new SqlParameters in the code,...
4
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
1
by: John Kotuby | last post by:
Hi all, I am working on porting an application from VB6 to VB.NET 2003 and am running into some problems. When declaring and populating the parameters for a SQL Stored Procedure by using the...
0
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
2
by: Jared Grant | last post by:
I am trying to find the value from some output parameters from a stored procedure. I have tried several different methods but somehow cannot get it to work. here is my source code: dim dr as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.