473,491 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference indirection with % confusion in c++/cli

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
Nov 17 '05 #1
4 1992
Don Kim wrote:
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;


--
Ioannis Vranos
Nov 17 '05 #2
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote
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/archi.../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
Nov 17 '05 #3
Don Kim wrote:
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


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
Nov 17 '05 #4
Don Kim wrote:
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote
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

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/archi.../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. :-)


--
Ioannis Vranos
Nov 17 '05 #5

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

Similar topics

5
10435
by: Nobody | last post by:
excuse the "windows" code below, but this is a C++ question... or maybe a C. Anyways, I want a function where I can pass in an array of strings (variable count, variable size), so I defined this:...
110
9806
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
14
4281
by: Composer | last post by:
I've read many postings about the problem of Access.References.IsBroken and the consensus seems to be that late binding is the cure-all. I have a very complex Access application that needs...
5
1834
by: Steve | last post by:
newbie question. In C++ I could get a pointer to an item in a list, for example int* pInt = &myList; then I could do something like: *pInt = 666; and then myList would equal 666. Nice,...
13
2747
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
12
5365
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
5
2675
by: Marty | last post by:
I am wondering what is the difference in the placement between the 2 uses of the indirection operator? Or is there a difference? AcDbBlockTable *pBlockTable = NULL; AcDbDatabase* pDB =...
68
4549
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
275
12024
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7118
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
6980
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
7157
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
7192
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
5452
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
3087
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
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
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 ...
1
637
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.