473,566 Members | 2,924 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 5451

"Subodh" <su***********@ gmail.comwrote in message
news:11******** **************@ 19g2000hsx.goog legroups.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.no spamwrote:
"Subodh" <subodh.bor...@ gmail.comwrote in message

news:11******** **************@ 19g2000hsx.goog legroups.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::GetAReferenc e(void)
{
return *m_obj
}

const A^ B::GetAHandle(v oid)
{
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.GetAReferenc e() 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******** *************@k 79g2000hse.goog legroups.com...
On Aug 28, 10:42 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
>"Subodh" <subodh.bor...@ gmail.comwrote in message

news:11******* *************** @19g2000hsx.goo glegroups.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::GetAReferenc e(void)
{
return *m_obj
}

const A^ B::GetAHandle(v oid)
{
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.GetAReferenc e() 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
14916
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
20
2103
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 everyone! Christopher Diggins http://www.cdiggins.com
9
4771
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not...
25
2906
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 construction and then.. // some other processing and/or changing 'retval' return retval; }
17
3577
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
3
1587
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 _c;
4
6677
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
29
2179
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 to use a union for that argument. In a header file there is the following: #define VAL_ALT union urt_value_alt .....
5
1977
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 function, and then figure that the function will not be modifying the list at all, so a const qualifier seems appropriate in the parameter. So...
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.