473,473 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

CONST ???

I just ran into something that absolutely blows my mind! Consider the
following:

class Class1 {
public:

void SetHours (const double& h) { Hours = h; }

double Hours;
};

public __gc class Class2 {
public:

double Hours;
};

Now, in C++ Managed extensions do this:

Class1 c1;
Class2 c2;

c1.Hours = c2.hours; // Works fine
c1.SetHours (c2.Hours); // Fails

The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
from 'double' to 'const double &'".

Say What!!??!!

Somewhere it says this is because c2.Hours is a managed type. Why
should that matter? This breaks the whole const mechanism. In the
case above I can get around it by using the direct copy (c1.Hours =
c2.hours), but suppose the members of Class1 were protected or
private? The whole reason for using const is to tell the compiler
that it cannot modify the input value. Why in the world would
Microsoft want to break this mechanism????

To get around this I must modify all the access member functions of my
unmanaged DLL to not use const? Or create a whole bunch of
intermediate variables and do a double copy? It does not make any
sense. Why doesn't the compiler handle that job automatically?

Russ
Nov 16 '05 #1
5 1413
Oops. never mind. Just realized that I posted this in the wrong group.
I will repost elsewhere.

Russ

On Wed, 07 Jul 2004 16:07:48 -0400, Russ <ru****@eticomm.net> wrote:
I just ran into something that absolutely blows my mind! Consider the
following:

class Class1 {
public:

void SetHours (const double& h) { Hours = h; }

double Hours;
};

public __gc class Class2 {
public:

double Hours;
};

Now, in C++ Managed extensions do this:

Class1 c1;
Class2 c2;

c1.Hours = c2.hours; // Works fine
c1.SetHours (c2.Hours); // Fails

The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
from 'double' to 'const double &'".

Say What!!??!!

Somewhere it says this is because c2.Hours is a managed type. Why
should that matter? This breaks the whole const mechanism. In the
case above I can get around it by using the direct copy (c1.Hours =
c2.hours), but suppose the members of Class1 were protected or
private? The whole reason for using const is to tell the compiler
that it cannot modify the input value. Why in the world would
Microsoft want to break this mechanism????

To get around this I must modify all the access member functions of my
unmanaged DLL to not use const? Or create a whole bunch of
intermediate variables and do a double copy? It does not make any
sense. Why doesn't the compiler handle that job automatically?

Russ


Nov 16 '05 #2
"Russ" <ru****@eticomm.net> wrote in message
news:jr********************************@4ax.com...
I just ran into something that absolutely blows my mind! Consider the
following:

class Class1 {
public:

void SetHours (const double& h) { Hours = h; }

double Hours;
};

public __gc class Class2 {
public:

double Hours;
};

Now, in C++ Managed extensions do this:

Class1 c1;
Class2 c2;

c1.Hours = c2.hours; // Works fine
c1.SetHours (c2.Hours); // Fails

The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
from 'double' to 'const double &'".

Say What!!??!!

Somewhere it says this is because c2.Hours is a managed type. Why
should that matter? This breaks the whole const mechanism. In the
case above I can get around it by using the direct copy (c1.Hours =
c2.hours), but suppose the members of Class1 were protected or
private? The whole reason for using const is to tell the compiler
that it cannot modify the input value. Why in the world would
Microsoft want to break this mechanism????

To get around this I must modify all the access member functions of my
unmanaged DLL to not use const? Or create a whole bunch of
intermediate variables and do a double copy? It does not make any
sense. Why doesn't the compiler handle that job automatically?


I'm not a managed c++ writer, but I'd say that C++-references and C# objects
don't mix (I mean to say that C++ references won't bind to a garabage
collected class or member of it) because of garbage collection.

Cheers,
---
Tom Tempelaere
Nov 16 '05 #3
"TT (Tom Tempelaere)" <_/\/_0§P@|/\|titi____AThotmail.com|/\|@P§0_/\/_>
wrote in message news:7u**********************@phobos.telenet-ops.be...
"Russ" <ru****@eticomm.net> wrote in message
news:jr********************************@4ax.com...
I just ran into something that absolutely blows my mind! Consider the
following:

class Class1 {
public:

void SetHours (const double& h) { Hours = h; }

double Hours;
};

public __gc class Class2 {
public:

double Hours;
};

Now, in C++ Managed extensions do this:

Class1 c1;
Class2 c2;

c1.Hours = c2.hours; // Works fine
c1.SetHours (c2.Hours); // Fails

The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
from 'double' to 'const double &'".

Say What!!??!!

Somewhere it says this is because c2.Hours is a managed type. Why
should that matter? This breaks the whole const mechanism. In the
case above I can get around it by using the direct copy (c1.Hours =
c2.hours), but suppose the members of Class1 were protected or
private? The whole reason for using const is to tell the compiler
that it cannot modify the input value. Why in the world would
Microsoft want to break this mechanism????

To get around this I must modify all the access member functions of my
unmanaged DLL to not use const? Or create a whole bunch of
intermediate variables and do a double copy? It does not make any
sense. Why doesn't the compiler handle that job automatically?
I'm not a managed c++ writer, but I'd say that C++-references and C#

objects don't mix (I mean to say that C++ references won't bind to a garabage
collected class or member of it) because of garbage collection.


Perhaps this would be possible in an unsafe code section, or if you pin the
object before binding the reference. But again, I'm not a managed C++-writer
so you'll have to double check me on this.

Cheers,
---
Tom Tempelaere
Nov 16 '05 #4
Tom, thanks for your comments. I appreciate it because I got no
response at all in the framework NG.

The thing is, that the access function:

void SetHours (const double& h) { Hours = h; }

is an inline function, meaning that in a release mode compile it
resolves to a direct assignment. So "c1.SetHours (c2.Hours);" becomes
"C1.Hours = C2.Hours". Notice that this is exactly the code that DOES
work. So it seems like an artifical rejection.

Also, even if it was not an inline function the compiler could simply
create a temporary unmanaged local variable, making it equivalent to:

double temp = C2.Hours;
C1.SetHours (temp);

Of course I can do that too, but that should be the job of the
compiler.

Thanks again, Russ
On Wed, 07 Jul 2004 22:09:36 GMT, "TT \(Tom Tempelaere\)"
<_/\/_0§P@|/\|titi____AThotmail.com|/\|@P§0_/\/_> wrote:
"TT (Tom Tempelaere)" <_/\/_0§P@|/\|titi____AThotmail.com|/\|@P§0_/\/_>
wrote in message news:7u**********************@phobos.telenet-ops.be...
"Russ" <ru****@eticomm.net> wrote in message
news:jr********************************@4ax.com...
> I just ran into something that absolutely blows my mind! Consider the
> following:
>
> class Class1 {
> public:
>
> void SetHours (const double& h) { Hours = h; }
>
> double Hours;
> };
>
> public __gc class Class2 {
> public:
>
> double Hours;
> };
>
> Now, in C++ Managed extensions do this:
>
> Class1 c1;
> Class2 c2;
>
> c1.Hours = c2.hours; // Works fine
> c1.SetHours (c2.Hours); // Fails
>
> The error is "C2664: 'Class1::SetHours' : cannot convert parameter 1
> from 'double' to 'const double &'".
>
> Say What!!??!!
>
> Somewhere it says this is because c2.Hours is a managed type. Why
> should that matter? This breaks the whole const mechanism. In the
> case above I can get around it by using the direct copy (c1.Hours =
> c2.hours), but suppose the members of Class1 were protected or
> private? The whole reason for using const is to tell the compiler
> that it cannot modify the input value. Why in the world would
> Microsoft want to break this mechanism????
>
> To get around this I must modify all the access member functions of my
> unmanaged DLL to not use const? Or create a whole bunch of
> intermediate variables and do a double copy? It does not make any
> sense. Why doesn't the compiler handle that job automatically?


I'm not a managed c++ writer, but I'd say that C++-references and C#

objects
don't mix (I mean to say that C++ references won't bind to a garabage
collected class or member of it) because of garbage collection.


Perhaps this would be possible in an unsafe code section, or if you pin the
object before binding the reference. But again, I'm not a managed C++-writer
so you'll have to double check me on this.

Cheers,
---
Tom Tempelaere


Nov 16 '05 #5
"Russ" <ru****@eticomm.net> wrote in message
news:q3********************************@4ax.com...
Tom, thanks for your comments. I appreciate it because I got no
response at all in the framework NG.
That would not be the group to ask. You should try

microsoft.public.dotnet.languages.vc

for managed C++.
The thing is, that the access function:

void SetHours (const double& h) { Hours = h; }

is an inline function, meaning that in a release mode compile it
resolves to a direct assignment. So "c1.SetHours (c2.Hours);" becomes
That is not an issue. The C++ language specs need to be in effect, otherwise
it wouldn't be C++ anymore. So inlining should have the same observable
behaviour (C++ rules) and in this case that is not the case. I think that
this is the issue here, although I'm neither a C++ language nor MS-managed
C++ expert so you should really ask someone else.
"C1.Hours = C2.Hours". Notice that this is exactly the code that DOES
work. So it seems like an artifical rejection.
Also, even if it was not an inline function the compiler could simply
create a temporary unmanaged local variable, making it equivalent to:

double temp = C2.Hours;
C1.SetHours (temp);

Of course I can do that too, but that should be the job of the
compiler.


Indeed, and a C++ compiler has strict rules to adhere to. If observable
behaviour would change due to inlining then that would be a problem.

Cheers,
---
Tom Tempelaere


Nov 16 '05 #6

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

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
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 =...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
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.