Reference indirection with % confusion in c++/cli

D

Don Kim

I have the following code:

using namespace System;

ref class R
{
public:

R()
{
Console::WriteLine("In R");
}
};

int main()
{
R ^r = gcnew R;

Object ^o;

//o = %r; //causes errors.
o = r;

o = gcnew R;
}

I wrote this to understand how ^ relates to %, as * relates to & in native
c++.

But trying to reference ^r with o indirectly with %r gives compiler errors.
Any ideas?

-Don Kim
 
I

Ioannis Vranos

Don said:
I have the following code:

using namespace System;

ref class R
{
public:

R()
{
Console::WriteLine("In R");
}
};

int main()
{
R ^r = gcnew R;

Object ^o;

//o = %r; //causes errors.
o = r;

o = gcnew R;
}

I wrote this to understand how ^ relates to %, as * relates to & in native
c++.

But trying to reference ^r with o indirectly with %r gives compiler errors.
Any ideas?




r is a handle. Handles are not managed types so you can use &r to take a
pointer to a handle of type R (R ^ *):


R ^r = gcnew R;

R ^ *o= &r;
 
D

Don Kim

Ioannis Vranos said:
r is a handle. Handles are not managed types so you can use &r to take a
pointer to a handle of type R (R ^ *):


R ^r = gcnew R;

R ^ *o= &r;

This still does not work. I got the following error:

error C2143: syntax error : missing ';' before '}'
error C2373: 'o' : redefinition; different type modifiers

Your example seems to imply the abilty for C++/CLI to declare copy
constructors, and I don't think C++/CLI allows this. Here's a blog from
Stan Lippman about this:

http://blogs.msdn.com/slippman/archive/2004/01/20/60655.aspx

"The CLR reflects a different programming philosophy and tradition - think
of SmallTalk as a point of origin, although that is not deeply thought out.
However, in the the presence of garbage collection and intrinsic
polymorphism of managed reference types [with default shallow copy], the
entire copy constructor/copy operator mechanism is not really necessary - at
least in the opinion of the inventors of the CLR, and I tend to agree.
However, it is felt as a bereavement by native C++ programmers, and it is a
pattern of usage should one wish to chase the grail of transparent code that
compiles to either native or managed. It falls between the intrinsics of
multiple inheritance and that of deterministic finalization in my opinion as
to its importance in being simulated. "

Also, my assumption was that handles are managed types, since they are
allocated on the managed heap and later deallocated by the GC. The Lippman
blog seems to agree with this.

Anyway, my understanding of c++/cli is still raw, so I may be wrong.

-Don Kim
 
T

Tamas Demjen

Don said:
This still does not work. I got the following error:

error C2143: syntax error : missing ';' before '}'
error C2373: 'o' : redefinition; different type modifiers

It appears you didn't download the VC++ 2005 Tools Refresh
(http://tinyurl.com/5mdq2). Without that you don't have half of the new
C++/CLI features. This should compile after installing the update.

Tom
 
I

Ioannis Vranos

Don said:
This still does not work. I got the following error:

error C2143: syntax error : missing ';' before '}'
error C2373: 'o' : redefinition; different type modifiers


Probably you have an older beta than mine (I downloaded CTP and use it
from command line only, the GUI crashes :) ).

Your example seems to imply the abilty for C++/CLI to declare copy
constructors, and I don't think C++/CLI allows this.


You can, but they are explicit copy constructors (CLI restriction). I
think that for this reason the keyword explicit should be used in
C++/CLI and it is one of the (few) things that I do not like in current
C++/CLI - not using the keyword explicit.




Here's a blog from
Stan Lippman about this:

http://blogs.msdn.com/slippman/archive/2004/01/20/60655.aspx

"The CLR reflects a different programming philosophy and tradition - think
of SmallTalk as a point of origin, although that is not deeply thought out.
However, in the the presence of garbage collection and intrinsic
polymorphism of managed reference types [with default shallow copy], the
entire copy constructor/copy operator mechanism is not really necessary - at
least in the opinion of the inventors of the CLR, and I tend to agree.
However, it is felt as a bereavement by native C++ programmers, and it is a
pattern of usage should one wish to chase the grail of transparent code that
compiles to either native or managed. It falls between the intrinsics of
multiple inheritance and that of deterministic finalization in my opinion as
to its importance in being simulated. "

Also, my assumption was that handles are managed types, since they are
allocated on the managed heap and later deallocated by the GC. The Lippman
blog seems to agree with this.

Anyway, my understanding of c++/cli is still raw, so I may be wrong.



Yes it is raw indeed. Also that blog entry is relatively old. So far
under latest VC++ Beta you can not get a handle to a handle but a
pointer to a handle, that is the handle itself is probably a value type
(or less likely unmanaged like a pointer).


Value types do not derive from Object. Consider this:


value class SomeClass
{};


int main()
{
SomeClass obj;

SomeClass *p= &obj;
}


C:\c>cl /clr temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.41013
for Microsoft (R) .NET Framework version 2.00.41013.0
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 8.00.41013
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>



Definitely we will get a head ache until we learn all C++/CLI. :)
 

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