Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2004

Tip: Looking for answers? Try searching our database.

out/ref params

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Cheng - 01 May 2004 02:40 GMT
Hi,

Does MC++ support out/ref param semantics (as C# does)?  The MC++ spec
doesn't mention about this, so supposedly they are not supported?

Thanks,
Simon
Jochen Kalmbach - 01 May 2004 07:46 GMT
> Does MC++ support out/ref param semantics (as C# does)?  The MC++ spec
> doesn't mention about this, so supposedly they are not supported?

Yes.

For ref:
MC++: void DoSth(
 Int32* pn,
 System::String** ps);

C#: void DoSth(ref Int32 pn, ref System.String ps);

For out:
MC++: void DoSth(
 [System::Runtime::InteropServices::OutAttribute] Int32* pn,
 [System::Runtime::InteropServices::OutAttribute] System::String** ps);

C#: void DoSth(out Int32 pn, out System.String ps);

Signature

Greetings
 Jochen

  Do you need a memory-leak finder ?
  http://www.codeproject.com/tools/leakfinder.asp
 
  Do you need daily reports from your server?
  http://sourceforge.net/projects/srvreport/

Simon Cheng - 01 May 2004 08:52 GMT
They don't enforce C#'s out/ref semantics.  For example, the following MC++
code compiles fine:

   void F1([System::Runtime::InteropServices::OutAttribute] Int32* pi) {}
   void F2(Int32* pi) {}

   int main()
   {
       Int32 i, j;
       F1(&i);
       F2(&j);
   }

But the equivalent C# code would fail compilation because:

   - F1() doesn't write to *pi before returning.
   - j is not written before passed to F2().

Simon

> > Does MC++ support out/ref param semantics (as C# does)?  The MC++ spec
> > doesn't mention about this, so supposedly they are not supported?
[quoted text clipped - 14 lines]
>
> C#: void DoSth(out Int32 pn, out System.String ps);
Jochen Kalmbach - 01 May 2004 09:09 GMT
> They don't enforce C#'s out/ref semantics.  For example, the following
> MC++ code compiles fine:
>
>     void F1([System::Runtime::InteropServices::OutAttribute] Int32*
>     pi) {}

H? !?
This is wrong... it will compile but it is wrong..
you have to return an value, for example  "*pi = 12"...

> void F2(Int32* pi) {}

This is ok.

> But the equivalent C# code would fail compilation because:
>
>     - F1() doesn't write to *pi before returning.
>     - j is not written before passed to F2().

H? !?
Please provide a full example...

Signature

Greetings
 Jochen

  Do you need a memory-leak finder ?
  http://www.codeproject.com/tools/leakfinder.asp
 
  Do you need daily reports from your server?
  http://sourceforge.net/projects/srvreport/

Simon Cheng - 04 May 2004 08:40 GMT
My original question was if MC++ supports C#'s out/ref semantics, which I
also meant if MC++ compiler _enforces_ the restrictions to out/ref as C#
compiler does -- that part I think I was not clear enough up-front.  For
example, the following C# code gives compilation errors, which is as
expected:

   class MyApp
   {
       static void F1(out int i) {}  // Error CS0177
       static void F2(ref int i) {}

       public static void Main()
       {
           int i, j;
           F1(out i);
           F2(ref j);  // Error CS0165
       }
   }

   Error CS0177: The out parameter 'i' must be assigned to before control
leaves the current method
   Error CS0165: Use of unassigned local variable 'j'

But the following MC++ code compiles without any error:

   #using <mscorlib.dll>

   void F1([System::Runtime::InteropServices::OutAttribute] int* pi) {}
   void F2(int* pi) {}

   int main()
   {
       int i, j;
       F1(&i);
       F2(&j);
   }

So, supposedly MC++ has no facility that is semantically equivelant to C#'s
out/ref?

Simon

> > They don't enforce C#'s out/ref semantics.  For example, the following
> > MC++ code compiles fine:
[quoted text clipped - 17 lines]
> H? !?
> Please provide a full example...
Jochen Kalmbach - 04 May 2004 08:53 GMT
> But the following MC++ code compiles without any error:
>
[quoted text clipped - 12 lines]
> So, supposedly MC++ has no facility that is semantically equivelant to
> C#'s out/ref?

Only the compiler does not ENFORCE this. But this is common to all C++
compilers that you can do more or less if you want...

C++ also does not restrict casting... or use of void-pointers...

If you want a type-safe and "language-safe" language you must use C# (or
any other langugate that supports what you want...)

Recapitulating we can say: C++ needs a higher learning curve that C#, and
you can do musch more "bad things" than with C#.

Signature

Greetings
 Jochen

 Do you need a memory-leak finder ?
 http://www.codeproject.com/tools/leakfinder.asp

 Do you need daily reports from your server ?
 http://sourceforge.net/projects/srvreport/

Simon Cheng - 04 May 2004 18:09 GMT
That's the reason I am wondering if MC++ (as opposed to C++) can enforce
C#'s out/ref semantic, as MC++ has already provided some other C# facilities
via additional keywords.  I am working on some code that needs to be done in
MC++, and equivalent facility to enforce out/ref would be nice.

Simon

> > But the following MC++ code compiles without any error:
> >
[quoted text clipped - 23 lines]
> Recapitulating we can say: C++ needs a higher learning curve that C#, and
> you can do musch more "bad things" than with C#.
Doug Harrison [MVP] - 04 May 2004 18:28 GMT
>They don't enforce C#'s out/ref semantics.  For example, the following MC++
>code compiles fine:
[quoted text clipped - 13 lines]
>    - F1() doesn't write to *pi before returning.
>    - j is not written before passed to F2().

Compilers are free to ignore the OutAttribute. The C# language respects the
attribute, and C# disallows the F2 call because of its "definite assignment"
requirement. C++ supports neither feature.

Signature

Doug Harrison
Microsoft MVP - Visual C++


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.