casting __value struct to __gc interface

  • Thread starter Thread starter Jose Ines Cantu Arrambide
  • Start date Start date
J

Jose Ines Cantu Arrambide

Hello,

How can i cast a __value structo to a __gc interface?
The code below throws a compiler error:

Person oPerson = Person::Person();

IPerson* iPerson = __try_cast<IPerson*>(oPerson);



Thanks.

Jose.
 
Is that c++? I never heard of a "try cast".

The only casts I know of are:
static_cast
reinterpret_cast
const_cast
dynamic_cast

Asside from that, you might not be able to cast a Person to an IPerson if
they are not related.

You also might not want to cast from a reference to a pointer. Perhaps this
might work:

IPerson* iPerson = __try_cast<IPerson*>(&oPerson);
 
Thanks Rudy,

IPerson* iPerson = __try_cast<IPerson*>(&oPerson);
I tried that but it doesnt work,... the only way it allows it is if I box
oPerson like this:

IPerson* iPerson = __try_cast<IPerson*>(__box(oPerson));
but iPerson wont point to oPerson and thats not what I was looking for.

do you know any other way to get an interface variable point to a struct?

Thanks,
Jose.
 
do you know any other way to get an interface
variable point to a struct?

No, I don't.

Additionally, I do not understand many of the things you are talking about,
like:
__try_cast
__box
__value structo
__gc

I do not see them listed in the book "The C++ Programming Language" by
Bjarne Stroustrup.
 
Its Managed c++

Rudy Ray Moore said:
No, I don't.

Additionally, I do not understand many of the things you are talking about,
like:
__try_cast
__box
__value structo
__gc

I do not see them listed in the book "The C++ Programming Language" by
Bjarne Stroustrup.
 
Jose Ines,
How can i cast a __value structo to a __gc interface?
The code below throws a compiler error:

Person oPerson = Person::Person();

IPerson* iPerson = __try_cast<IPerson*>(oPerson);

If Person does implement IPerson, it shouldn't throw any errors. Besides, if
it does, you wouldn't even need a cast, because an implicit conversion would
suffice.

What's the real code like, and what errors are you getting?
 
Thanks Tomas,

Iam getting error C2682: cannot use __try_cast to convert from
'Test::Person' to 'Test::IPerson __gc *'

IPerson* iPerson = __try_cast<IPerson*>(__box(oPerson));
if I use the code above it does work, but I dont get a reference to the
original struct, it creates a new one (a copy). I am trying to get a
reference.

Here is the code for the interface & struct :

#pragma once
using namespace System;
namespace Test{
public __gc __interface IPerson{
__property String* get_Name();
__property void set_Name(String* _sValue);
__property int get_Age();
__property void set_Age(int _iValue);
};

public __value struct Person : IPerson
{
public:
__property String* get_Name(){
return m_sName;
}
__property void set_Name(String* _sValue){
m_sName = _sValue;
}
__property int get_Age(){
return m_iAge;
}
__property void set_Age(int _iValue){
m_iAge = _iValue;
}
String* ToString(){
return Name;
}
private:
int m_iAge;
String* m_sName;
};
}
 
Jose Ines,

Jose Ines Cantu Arrambide said:
Thanks Tomas,

Iam getting error C2682: cannot use __try_cast to convert from
'Test::Person' to 'Test::IPerson __gc *'

IPerson* iPerson = __try_cast<IPerson*>(__box(oPerson));
if I use the code above it does work, but I dont get a reference to the
original struct, it creates a new one (a copy). I am trying to get a
reference.

I would strongly urge you to avoid mutable interfaces on value types, it can
cause all kinds of unexpected problems!

You're right, though, in that casting it to an interface reference requires
boxing. On interesting (albeit old, and in C#) comment on this is
http://discuss.develop.com/archives/wa.exe?A2=ind0012B&L=DOTNET&P=R10747&I=-3
 
Back
Top