473,498 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing const objects as arguments to function or return types

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

Aug 28 '07 #1
3 5447

"Subodh" <su***********@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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
>
any help will be appreciated

Thanks and Regards,
Subodh

Aug 28 '07 #2
On Aug 28, 10:42 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"Subodh" <subodh.bor...@gmail.comwrote in message

news:11**********************@19g2000hsx.googlegro ups.com...
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
any help will be appreciated
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

Aug 28 '07 #3

"Subodh" <su***********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On Aug 28, 10:42 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>"Subodh" <subodh.bor...@gmail.comwrote in message

news:11**********************@19g2000hsx.googlegr oups.com...
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
any help will be appreciated
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?
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.
Aug 28 '07 #4

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

Similar topics

3
14902
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
20
2092
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance...
9
4764
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
25
2896
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
17
3567
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
3
1580
by: KK | last post by:
Hello all, I have several classes binded by one common interface - say 'sum' interface which calculates the sum of all the class elements of type 'int'. class Alphabet { int _a; int _b; int...
4
6670
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
29
2169
by: Richard Harter | last post by:
There is probably a simple way to do what I want but I don't see it. Any suggestions are welcome. Suppose I have a function foo with an argument that can be any of several types and that I want...
5
1964
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
0
7125
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
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
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
7208
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...
1
6890
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
7379
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...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.