Passing const objects as arguments to function or return types

S

Subodh

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
 
B

Ben Voigt [C++ MVP]

Subodh said:
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.

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

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

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

Subodh

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.





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


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
 
B

Ben Voigt [C++ MVP]

Subodh said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top