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++ / August 2007

Tip: Looking for answers? Try searching our database.

Passing const objects as arguments to function or return types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Subodh - 28 Aug 2007 18:29 GMT
Hi All,

In C++ we could pass a constant reference to a function so that the
target function could not modify the objects passed
eg.  const classA & dummmyfunction(const classB)
similar thing was valid for objects passed with pointer to constant
objects

In C++/CLI is there any way for imitating this, I want to pass
arguments to and return value from my member function as a constant
objects.

currently i am passing a handle to the object to client (consumer)
function which then can easily modify the object received

I had one more doubt about C++/CLI
In C++/CLI Handles are similar(may be not not equivalent) to that for C
++ pointers
Is there any operator that is equivalent to C++ References (&) in C++/
CLI

any help will be appreciated

Thanks and Regards,
Subodh
Ben Voigt [C++ MVP] - 28 Aug 2007 18:42 GMT
> Hi All,
>
[quoted text clipped - 7 lines]
> arguments to and return value from my member function as a constant
> objects.

Sadly no, the compiler throws an error as soon as you try to declare const
member functions, so passing handles to const is pretty useless: you can't
even read properties.

> currently i am passing a handle to the object to client (consumer)
> function which then can easily modify the object received
[quoted text clipped - 4 lines]
> Is there any operator that is equivalent to C++ References (&) in C++/
> CLI

The tracking reference (%) is the version of the C++ reference that supports
garbage collection

> any help will be appreciated
>
> Thanks and Regards,
> Subodh
Subodh - 28 Aug 2007 19:39 GMT
> > Hi All,
>
[quoted text clipped - 28 lines]
> > Thanks and Regards,
> > Subodh

If I use a tracking reference in an API in my C++/CLI program then I
am not able to use this in C# client application it says "function is
not supported by the language "

Consider A and B are two classes in my C++/CLI project
public ref class B
{

const A% B::GetAReference(void)
{
return *m_obj
}

const A^ B::GetAHandle(void)
{
return m_obj;
}

private:
A^ m_aobj
};

now in my C# client:
1.  if i use a call to GetAReference() function, I get a compile time
Error:  "B.GetAReference() is not supported by the language"
any idea why is this not supported?

2 GetAHandle() works fine, but now the C# client does not treats the
object returned as a constant object whereas in C++/CLI definition I
have mentioned returntype as const handle to A
i.e. it allows me to modify the A object
--- Is there any way to restrict this, I.e. pass a const object to a
C# client App from a C++/CLI function

Any help will be appreciated

Thanks and regards,
Subodh
Ben Voigt [C++ MVP] - 28 Aug 2007 20:35 GMT
>> > Hi All,
>>
[quoted text clipped - 58 lines]
> Error:  "B.GetAReference() is not supported by the language"
> any idea why is this not supported?

Because Microsoft didn't think it was important enough to put in?  Or was
potentially too confusing for their target audience for C#, the millions of
VB6 users who did more drag-and-drop than writing code?

> 2 GetAHandle() works fine, but now the C# client does not treats the
> object returned as a constant object whereas in C++/CLI definition I
> have mentioned returntype as const handle to A
> i.e. it allows me to modify the A object
> --- Is there any way to restrict this, I.e. pass a const object to a
> C# client App from a C++/CLI function

No, the CLR does not implement const-correctness.

Rate this thread:







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.