out equivalent in MC++

  • Thread starter Thread starter 011
  • Start date Start date
0

011

Hello, All!

What is C# "out" keyword equivalent in MC++? Also I need "ref".

Regards,
011.

Winamp 5.0 (playing): Stratovarius - Babylon
 
There is no such thing as an "out" method parameter in C++. You achieve the
same thing by using a reference (&). See the below example:

//C# ===============================
void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i = 'b';
}

char i = 'a'; // variable must be initialized
TestRef(ref i); // the arg must be passed as ref


//C++ ==============================
void TestRef(char& i)
{
// The value of i will be changed in the calling method
i = 'b';
}

char i = 'a';
TestRef(i);
 
There is no such thing as an "out" method parameter in C++.

That ain't exactly true. In MC++, it is possible to generate a method
signature that means "out", by using __gc pointers (or pointers to pointers
in the case of reference types) and the
System::Runtime::InteropServices::OutAttribute attribute.
 
1845633626

Hello, Tomas!
You wrote on Wed, 17 Mar 2004 18:59:01 -0500:

TRM> That ain't exactly true. In MC++, it is possible to generate
TRM> a method signature that means "out", by using __gc pointers
TRM> (or pointers to pointers in the case of reference types) and
TRM> the System::Runtime::InteropServices::OutAttribute attribute.

Any example?

I think that it simpler to declare Object*& to have "ref" that to use that attribute. There's no much difference between out and ref.

Regards,
011.

Winamp 5.0 (not active)
 
Hello, Daniel!
You wrote on Wed, 17 Mar 2004 14:41:34 -0600:

DP>>> There is no such thing as an "out" method parameter in C++.
DP>>> You achieve the same thing by using a reference (&). See the
DP>>> below example:

What about Object*&?

Regards, 011.

Winamp 5.0 (not active)
 
In your example it is a reference to a pointer.

1. CMyClass& myObject

is the reference to the object. You can acces it in your method by using "."
(period)

myObject.OneMethod();

while

2. CMyClass*& pmyObject

is a reference to a pointer to the object. You can acces it in your method
by uisng "->"

pmyObject->OneMethod();


The difference between #1 and #2 is the type the reference refers to. In #1
it refers to an object of type CMyClass. In #2 it referes to a pointer to
CMyClass.


A reference is another name for an existing object. For example:

CMyClass obj1;
CMyClass& myObj = obj1;

If you modify myObj then obj1 will be modified as well.

You cannot declare a reference without "attaching" it to an already existing
object. For example this is invalid:

CMyClass& myObj;
 
Yeah, when I answered I was thinking at the standard C++.

Anyway I do not see why anyone would use
System::Runtime::InteropServices::OutAttribute attribute.

when it can use a reference or a pointer unless it has to write a piece of
code that needs to work with C# and it this case probably it will use your
solution.
 
Hi Danil,
Yeah, when I answered I was thinking at the standard C++.

Anyway I do not see why anyone would use

when it can use a reference or a pointer unless it has to write a piece of
code that needs to work with C# and it this case probably it will use your
solution.

Ahh, that ;) Well, explicitness would be one reason (and if you're writing
managed code, then you *should* be thinking about how things are going to
look from the outside, or at least that's my opinion).
 
There is no such thing as an "out" method parameter in C++.
TRM> That ain't exactly true. In MC++, it is possible to generate
TRM> a method signature that means "out", by using __gc pointers
TRM> (or pointers to pointers in the case of reference types) and
TRM> the System::Runtime::InteropServices::OutAttribute attribute.

Any example?

I think that it simpler to declare Object*& to have "ref" that to use that
attribute. There's no much difference between out and ref.

True, but then again, it makes it explicit what's going on and what's
expected of the method, which I personally think it's nice.

So, basically, you can use either Object*& or Object** to do the "ref"
thingie for reference types (with the actual type, of course, no need to use
Object explicitly); either one is exactly the same at the IL level, only
difference being an extra C++ specific custom attribute inserted by the
compiler for the *& case (which is only meaningful for the MC++ compiler,
the rest of languages ignore it).

For "out", either one would work, just add the [Out] attribute:

#using <mscorlib.dll>


using namespace System;
using namespace System::Runtime::InteropServices;

public __gc class A
{
public:
static void GetA([Out]A** a) { *a = new A(); }
};

int main()
{
A* a; A::GetA(&a);
}


This will now be understood as A.GetA(out A a) by the C# compiler.
 
Hi Tomas,

Yes, you are right but I'm just tired of learning every small detail of a
Microsoft technology just to discover after 2-3 years that it was thrown
away by MS and replaced with something more "cool".

I've spent a lot money and time learning about COM just to use it a couple
of times. Now, COM is (almost) dead, long live .NET, and so on.

I made the decision to learn more about C++, Object Oriented Analysis and
Design, UML, Rational Unified Process and other knowledge areas that are
tehcnology independend.

BTW: I went recently to a meeting with Bjarne Stroustrup and in less than 2
hours I discovered that after 10 years of C++ I still have a lot to learn.
The guy talked about function objects (functors) and generic programming.
 
Daniel P. said:
Hi Tomas,

Yes, you are right but I'm just tired of learning every small detail of a
Microsoft technology just to discover after 2-3 years that it was thrown
away by MS and replaced with something more "cool".

I've spent a lot money and time learning about COM just to use it a couple
of times. Now, COM is (almost) dead, long live .NET, and so on.

I made the decision to learn more about C++, Object Oriented Analysis and
Design, UML, Rational Unified Process and other knowledge areas that are
tehcnology independend.

BTW: I went recently to a meeting with Bjarne Stroustrup and in less than 2
hours I discovered that after 10 years of C++ I still have a lot to learn.
The guy talked about function objects (functors) and generic programming.

Well spoken Daniel. I have jumped though so many hoops with MS's latest
"cool" technologies over the years, I can't see straight. As you, I am
sticking with C++, OO process, UML, etc. I design and write code that is
independent of the latest "cool" MS toy, and move freely between the
linux/unix world and the MS coolland. If Visual C++ is finally extinguished
since they can't control the C++ standard, and they bury it with C#, C#++ or
whatever, I will move to an open source C++ implementation for Windows. I
know many others out there are equally fed up with the churn.
Our latest project (started last year) and running for the next decade+ is
C++/linux. Windows was not even a consideration due to the instability of
the constant technology shuffle. Good luck to you!
bob
 
Hello, Tomas!
You wrote on Thu, 18 Mar 2004 20:55:17 -0500:

TRM> For "out", either one would work, just add the [Out]
TRM> attribute:
TRM> #using <mscorlib.dll>
TRM> using namespace System;
TRM> using namespace System::Runtime::InteropServices;
TRM> public __gc class A {
TRM> public:
TRM> static void GetA([Out]A** a) { *a = new A(); }
TRM> };

Better this way: GetA([Out]A*& a) { a = new A(); }. No need for an additional asterisk(s).

Regards,
011.

Winamp 5.0 (playing): Stratovarius - Infinity
 
011 said:
1845633626

Hello, Tomas!
You wrote on Wed, 17 Mar 2004 18:59:01 -0500:

TRM> That ain't exactly true. In MC++, it is possible to generate
TRM> a method signature that means "out", by using __gc pointers
TRM> (or pointers to pointers in the case of reference types) and
TRM> the System::Runtime::InteropServices::OutAttribute attribute.

Any example?

I think that it simpler to declare Object*& to have "ref" that to use that attribute. There's no much difference between out and ref.

If you're calling remote methods, "out" will save some bandwidth because
you won't have to marshal the parameter in both directions.

Jesse
 
Back
Top